改变双轴图Y-ggplot2的比例

时间:2018-06-11 13:52:59

标签: r ggplot2 shiny scale ggplotly

我想研究天气对道路交通的影响 喜欢用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)) 

2 个答案:

答案 0 :(得分:0)

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")
然而,

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

enter image description here

答案 1 :(得分:0)

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

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

enter image description here