这里是你的数据清理,并编造值2015年12月一次数据被R读入内存,dput()
函数为数据帧df产生这个数据。
structure(list(Month = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L), .Label = c("Apr ", "Aug ", "Dec ", "Feb ", "Jan ",
"Jul ", "Jun ", "Mar ", "May ", "Nov ", "Oct ", "Sep "), class = "factor"),
Year = c(2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014), Visits = c(835, 895,
1117, 964, 968, 1120, 961, 1109, 1207, 1167, 1142, 1000,
675, 620, 713, 634, 617, 626, 668, 650, 744, 792, 710, 746
), Trials = c(41, 41, 46, 41, 42, 46, 46, 46, 48, 45, 41,
40, 31, 30, 33, 29, 30, 30, 31, 32, 33, 34, 32, 30)), .Names = c("Month",
"Year", "Visits", "Trials"), row.names = c(NA, -24L), class = "data.frame")
下一步是将数据帧融合成适合绘图的长格式。
library(reshape2)
df.m <- melt(df, id.vars = c("Year", "Month")) # put the data frame in long form
最后,情节,可以做很多方式,但这一个打破了几年成两个小组。情节图像在这里被挤压。
library(ggplot2)
ggplot(df.m, aes(x = Month, y = value, fill = variable)) +
geom_bar(stat = "identity", width = 0.7, position = "dodge") +
geom_text(aes(label = value), vjust = .4, position=position_dodge(width=0.8)) +
theme_bw() +
labs(x = "", y = "Number", title = "Two-Year Comparison of Visits and Trials") +
theme(axis.ticks= element_blank()) +
theme(plot.title = element_text(face = "bold", size = 11)) +
scale_y_continuous(label = NULL, expand = c(0, 0)) +
scale_fill_discrete(name="") +
theme(legend.position = "bottom") +
facet_wrap(~ Year)
Seano314 - 你尝试过这么远吗?另外,你可以把你的数据以一种可以轻松加载到R的格式,使用dput吗? – Nelson
尝试ggplot。 http://docs.ggplot2.org/current/ –