我想研究天气对道路交通的影响 喜欢用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))
答案 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")
然而,不需要辅助轴。所以要小心!!
答案 1 :(得分:0)