我正在尝试为每个ID(第1列)创建一个图表,在每个图表上按日期绘制foo和bar,并且条形图需要在倒置的轴上...
我的数据有表格
ID <- rep(6:10, times=5)
foo <-rnorm(n=25, mean=0, sd=1)
bar <-rnorm(n=25, mean=10, sd=1)
dayt <-rnorm(n=25, mean= 1, sd=1)
df <-data.frame(ID,dat,x,y)
我不知道从哪里开始除了我知道ggplot2允许多个对象轻松添加到图表中...
我正在尝试这样的事情
require(ggplot2)
require(plyr)
require(gridExtra)
pl <- dlply(df, .(ID), function(dat) {
ggplot(data = dat, aes(x = dayt, y = foo)) + geom_line() +
geom_point() + xlab("x-label") + ylab("y-label") +
geom_smooth(method = "lm")
})
ml <- do.call(marrangeGrob, c(pl, list(nrow = 5, ncol = 1)))
ggsave("my_plots.pdf", ml, height = 8, width = 11, units = "in")
但是无法弄清楚如何将第二个数据添加到每个图中以及反转轴......
任何帮助都会很棒!
感谢
ZR
答案 0 :(得分:1)
听起来你想创建一个简单的散点图,每个ID有一个多个图表,反转Y轴。
如果您想为每个ID创建一个包含多个图表的图表,您可以使用ggplot的分面函数(facet_grid
或facet_wrap
)。您可以使用scale_y_reverse()
功能反转Y轴。
以下是解决问题的一种方法:
library(ggplot2) # Load the library
p <- ggplot(df, aes(x=x, y=y)) + # Tell ggplot what you're plotting
geom_point() + # Tell ggplot it's a scatter plot
facet_wrap(~ ID) + # Plot one chart for each ID
scale_y_reverse() # Reverse the axis
p # Display the chart