简体   繁体   English

为什么我不能用 ggplot2 更改我的 y 轴值?

[英]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.我正在尝试使用 R 和 ggplot2 更改我的 y 轴,以便更好地查看过度播放的图。 As you can see Pakistan and United Kingdom in the first column, aren't readable正如您在第一列中看到的巴基斯坦和英国,不可读

在此处输入图像描述

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 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更新 - 基于下面的答案

在此处输入图像描述

What you probably meant to do was c(100,200,...) instead of seq(...) .您可能打算做的是c(100,200,...)而不是seq(...) Or c(seq(0,1000,by=100),seq(2100,3000,by=100)) .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.也就是说,绘图的整体 y 轴范围将与以前相同,您只需调整网格线的间距。 I would try omitting scale_y_continuous() (ie, letting ggplot use its default algorithm for choosing axis breaks) and specifying a free y-scale in facet_wrap() , ie我会尝试省略scale_y_continuous() (即,让ggplot使用其默认算法来选择轴中断)并在facet_wrap()中指定一个免费的 y 比例,即

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

在此处输入图像描述

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM