GaB
GaB

Reputation: 1134

Why I cannot change my y-axis values with ggplot2?

I am trying to change my y-axis for a better view of the overplayed plots, with R, with ggplot2. As you can see Pakistan and United Kingdom in the first column, aren't readable

enter image description here

here is the code:

sympt_count_plot <- ggplot2::ggplot(count_symptoms_positive_add, ggplot2::aes(x = age_band, y = Count, group = Symptoms, fill = Symptoms)) +
  ggplot2::geom_area( color = "white") +
  ggplot2::scale_x_discrete(limits = c( "0-9", "10-19", "20-29", "30-39", "40-49", "50-59", 
                                        "60+"), expand = c(0, 0)) +
  ggplot2::scale_fill_viridis_d() +
  ggplot2::scale_y_continuous(breaks = seq(0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,
                                                                       2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000)) +
  ggplot2::labs(#title = "% of responders across countries",
    y = "Count", x = "Age band") +
  theme(
    axis.text = element_text(size = 14),
    axis.title = element_text(size = 16),
    plot.title = ggplot2::element_text(size = 17, face = "bold"),
    plot.subtitle = ggplot2::element_text(size = 17),
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    strip.background = element_blank(),
    strip.text = element_text(size = 10, face = "bold", hjust = 0), 
    legend.title = element_text(size = 16), 
    legend.text = element_text(size = 13)) +
  ggplot2::facet_wrap(~country, ncol = 1)

sympt_count_plot

I get an error of this kind:

Error in seq.default(0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,  : 
  too many arguments
    In addition: Warning message:
    In seq.default(0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 
        2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000) :
     extra arguments  will be disregarded 

I have added the new values so that I would be able to see the plots better

And here is the data:

https://github.com/gabrielburcea/stackoverflow_fake_data/blob/master/count_symptoms_showing_symptoms_add.csv

Is there a way to fix this?

Update - based on the answer bellow

enter image description here

Upvotes: 0

Views: 453

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226731

What you probably meant to do was c(100,200,...) instead of seq(...). Or c(seq(0,1000,by=100),seq(2100,3000,by=100)). But I suspect this will not do what you want; that is, the overall y-axis range of the plots will be the same as before, you will only be adjusting the spacing of the grid lines. I would try omitting scale_y_continuous() (i.e., letting ggplot use its default algorithm for choosing axis breaks) and specifying a free y-scale in facet_wrap(), i.e.

facet_wrap(~country, ncol = 1, scale="free_y")

enter image description here

Full code:

count_symptoms_positive_add <- read.csv("SO65597881.csv")
library(ggplot2)
sympt_count_plot <- ggplot(count_symptoms_positive_add, aes(x = age_band, y = Count, group = Symptoms, fill = Symptoms)) +
  geom_area( color = "white") +
  scale_x_discrete(limits = c( "0-9", "10-19", "20-29", "30-39", "40-49", "50-59", 
                                        "60+"), expand = c(0, 0)) +
  scale_fill_viridis_d() +
  labs(#title = "% of responders across countries",
    y = "Count", x = "Age band") +
  theme(
    axis.text = element_text(size = 14),
    axis.title = element_text(size = 16),
    plot.title = element_text(size = 17, face = "bold"),
    plot.subtitle = element_text(size = 17),
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    strip.background = element_blank(),
    strip.text = element_text(size = 10, face = "bold", hjust = 0), 
    legend.title = element_text(size = 16), 
    legend.text = element_text(size = 13)) +
  facet_wrap(~country, ncol = 1, scale="free_y")

sympt_count_plot 
ggsave(type="cairo-png",file="sympt_count_plot.png")

Upvotes: 1

Related Questions