【发布时间】: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