0

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

GaB
  • 1,076
  • 2
  • 16
  • 29

1 Answers1

1

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")
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • I actually want to keep the same values to all of them. Is this possible? – GaB Jan 06 '21 at 14:58
  • Do you want an axis break, i.e. the space between 1000 and 3000 is omitted? This is hard (see https://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis ) and will be even harder with a derived plot like `geom_area()` ... – Ben Bolker Jan 06 '21 at 15:02
  • I totally get you. You're right here. is just that I was trying to make it uniform as I am adding another column, for comorbidities. and tried to have the same values across all overplayed plots including for the second column that will be added later. – GaB Jan 06 '21 at 15:03
  • c(seq(0,1000,by=100),seq(2100,3000,by=100)) - where exactly I pass this? – GaB Jan 06 '21 at 15:07
  • as your `breaks` argument (in place of `seq(...)`). But I don't think you'll like the results. – Ben Bolker Jan 06 '21 at 15:26
  • I am trying to offer this as an alternative. As you are right, probably we cannot achieve what I want as per explanation in stack overflow link you have provided. However, I am not sure how you managed to get clear y-axis, I could not. Check the updated post with the new version of the picture. – GaB Jan 06 '21 at 20:30
  • the picture? with the second overplayed plots? Do you see it? It does say "Update - based on the version of code provided." But it is situated after your plot. – GaB Jan 07 '21 at 15:01
  • 1
    I didn't see that you had edited my answer. Please edit **your question** to include this graph, not my answer ... – Ben Bolker Jan 07 '21 at 15:54
  • added to my post. Thank you – GaB Jan 07 '21 at 16:00
  • did not solve my issue with the y axis. Would you please put the whole code you have written in order to give the output? – GaB Jan 08 '21 at 16:16
  • @ Ben Bolker - I am hoping you saw the above last comment? – GaB Jan 10 '21 at 17:41