【问题标题】:change the scale of a two-axis graph Y - ggplot2更改两轴图 Y 的比例 - ggplot2
【发布时间】:2018-11-20 19:13:51
【问题描述】:

想研究天气对道路交通的影响,我会 喜欢做一个图表,x 轴,月份,右边的 Y 轴,温度和降水,左边的 Y 轴,汽车的数量。

但是,当我生成此图表时,降水和温度消失了,因为汽车数量轴的比例太重要了(从 0e + 00 到 3e + 06)。右侧 Y 轴的刻度是否可以从 0 到 30 不等,而左侧 Y 轴的刻度则不同?

为了更好的可视化,下面是示例代码。

structure(list(SOUNAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "WATERFORD (TYCOR)", class = "factor"), year_month = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("2013-03", "2013-05",
"2013-08", "2013-09", "2013-10", "2013-12"), class = "factor"),
    pre_type = structure(c(4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L
    ), .Label = c("HEAVY", "LIGHT", "MEDIUM", "NONE"), class = "factor"),
    pre_value = c(17L, 1L, 7L, 3L, 16L, 1L, 10L, 4L, 17L, 1L,
    8L, 5L, 12L, 1L, 11L, 6L, 7L, 3L, 16L, 5L, 6L, 2L, 20L, 3L
    ), tem_type = structure(c(4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
    3L), .Label = c("COLD", "HOT", "MEDIUM", "V_COLD"), class = "factor"),
    tem_value = c(0L, 15L, 0L, 16L, 0L, 28L, 3L, 0L, 0L, 0L,
    30L, 0L, 0L, 12L, 16L, 0L, 0L, 30L, 1L, 0L, 0L, 26L, 0L,
    5L), cnt_vehicle = c(NA, 2822180, NA, NA, NA, 3142510, NA,
    NA, NA, 3372690, NA, NA, NA, 3321950, NA, NA, NA, 3352332,
    NA, NA, NA, 3051846, NA, NA), x = c(1L, 1L, 1L, 1L, 2L, 2L,
    2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L,
    6L, 6L, 6L)), .Names = c("SOUNAME", "year_month", "pre_type",
"pre_value", "tem_type", "tem_value", "cnt_vehicle", "x"), row.names = c(NA,
-24L), class = "data.frame")

ggplot(data = w_complet_2013, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") +
  scale_x_continuous(breaks=1:6, labels=unique(w_complet_2013$year_month)) +
  geom_bar(stat = "identity", width=0.3) +
  xlab("date") + ylab ("Number of days of precipitation") +
  ggtitle("Precipitation per month") + labs(fill = "Frequency") +
  geom_bar(data=w_complet_2013,aes(x=x+0.4, y=tem_value, fill=tem_type), width=0.3, stat = "identity") +
  xlab("date") + ylab("Number of days of temperature") +
  ggtitle("Temperatures and precipitations- WATERFORD") + labs(fill = "Frequency") + theme( plot.title = element_text(size = 12, face ="bold.italic")) +
  geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), size = 1), show.legend=FALSE, stat = "identity") +
  scale_y_continuous(sec.axis = sec_axis(~.,name="Number of vehicle")) + theme( plot.title = element_text(size = 11, face ="bold.italic")) + theme(axis.title.x = element_text(size = 15)) + theme(axis.title.y = element_text(size = 15)) 

【问题讨论】:

    标签: r ggplot2 shiny scale ggplotly


    【解决方案1】:

    ggplot2 并不“真正”支持辅助 y 轴...但您可以通过将值相乘(例如:乘以 100000)来欺骗程序包,然后再通过除以 100000 来更改轴。

    ggplot(data = w_complet_2013, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") +
      scale_x_continuous(breaks=1:6, labels=unique(w_complet_2013$year_month)) +
      geom_bar(stat = "identity", width=0.3) +
      xlab("date") + ylab ("Number of days of precipitation") +
      ggtitle("Precipitation per month") + labs(fill = "Frequency") +
      geom_bar(data=w_complet_2013,aes(x=x+0.4, y=tem_value*100000, fill=tem_type), width=0.3, stat = "identity") +
      xlab("date") + ylab("Number of days of temperature", label = ) +
      ggtitle("Temperatures and precipitations- WATERFORD") + labs(fill = "Frequency") + theme( plot.title = element_text(size = 12, face ="bold.italic")) +
      geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), size = 1), show.legend=FALSE, stat = "identity") +
      scale_y_continuous(sec.axis = sec_axis(~.,name="Number of days of temperature")) + theme( plot.title = element_text(size = 11, face ="bold.italic")) + theme(axis.title.x = element_text(size = 15)) + theme(axis.title.y = element_text(size = 15)) +
      labs(y = "Number of vehicles")
    

    但是.. 不需要辅助轴。所以要小心!!

    【讨论】:

    • 非常感谢您的回答!你认为我可以用另一种方法来做这个图表吗?因为突然我的 Y1 轴(右)消失了……
    • 啊,但我刚刚注意到,这样做会丢失一半的温度和降水信息!
    【解决方案2】:

    由于您的代码有些错误,我删除了所有我认为不相关的内容。 此外,尺度确实不同。也许尝试日志。

    ggplot(data = d, aes(x = x, y = pre_value, fill = pre_type)) +
      geom_col(width=0.3) +
      geom_point(aes(y=log10(as.numeric(cnt_vehicle))), show.legend = F) + 
      scale_y_continuous(sec.axis = sec_axis(~10^., breaks = c(c(2,5)*10^6)))
    

    【讨论】:

      猜你喜欢
      • 2022-07-12
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多