Changing scales on y-axis of a bar graph

Hi,
I have a data for which I have plotted a bar graph. But I want the y-axis to be shown as % from 0 to 100. How can I do this?

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor

nit<-tibble::tribble(
  ~online_class,
  "Yes",
  "Yes",
  "Yes",
  "Yes",
  "Yes",
  "No",
  "Yes",
  "No",
  "Yes",
  "Yes",
  "Yes",
  "No"
)
ggplot(nit,aes(online_class))+
  geom_bar(aes(fill=online_class))+
  geom_text(stat = 'count',aes(label=percent(..count../nrow(nit))))

I would summaize the data before using ggplot() and use geom_col().

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor

nit<-tibble::tribble(
  ~online_class,
  "Yes",
  "Yes",
  "Yes",
  "Yes",
  "Yes",
  "No",
  "Yes",
  "No",
  "Yes",
  "Yes",
  "Yes",
  "No"
)
nit |> group_by(online_class) |> 
  summarize(Percent = n()/nrow(nit)) |> 
ggplot(aes(x = online_class, y = Percent, fill=online_class)) +
  geom_col() +
  geom_text(aes(label=percent(Percent))) +
  scale_y_continuous(labels = percent, limits = c(0,1)) 

Created on 2022-08-01 by the reprex package (v2.0.1)

I will just modify the data here. These are not of character type, but numeric as given below.
Is there something I can do so that I get count of each score and the scales are also in accordance with that?

library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
data<-tibble::tribble(
  ~student, ~l1c1_total,
    "S001",          1L,
    "S002",          2L,
    "S003",          2L,
    "S004",          1L,
    "S005",          0L,
    "S006",          0L,
    "S007",          5L
  )

data %>% 
  ggplot(aes(l1c1_total))+
  geom_bar(fill="orange",stat = "count",width = 0.5)+
  labs(title = "Performance in Identification and Sorting of shapes",
       x="Question score",
       y="% of students")+
  geom_text(stat='count',aes(label=percent(..count../nrow(data))),vjust=-0.5)+
  theme_minimal()


Created on 2022-08-01 by the reprex package (v2.0.1)

Sorry, I do not understand what you want. Do you want the y axis to display the count or the percent of the total counts. In the code you posted, you display the count but label it as a percentage.

On the y-axis I want to display %. Basically, this is the score of a few students. There are students with scores 0,1,2, etc. In the graph I want to display the % of students who got those particular scores. I hope it is clear now.

The code I originally posted will also work with your latest data.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor

data<-tibble::tribble(
  ~student, ~l1c1_total,
  "S001",          1L,
  "S002",          2L,
  "S003",          2L,
  "S004",          1L,
  "S005",          0L,
  "S006",          0L,
  "S007",          5L
)

data %>% group_by(l1c1_total) |> 
  summarize(Percent = n()/nrow(data)) |> 
  ggplot(aes(x = l1c1_total, y = Percent)) +
  geom_col(fill="orange", width = 0.5) +
  labs(title = "Performance in Identification and Sorting of shapes",
       x="Question score",
       y="% of students") +
  geom_text(aes(label = percent(Percent)),vjust=-0.5) +
  scale_y_continuous(labels = percent, limits = c(0,1)) +
  theme_minimal()

Created on 2022-08-01 by the reprex package (v2.0.1)

Thanks a lot for this. It is working now.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.