首页 文章

多个图表输出,每个都有多个数据,一个是相反的顺序

提问于
浏览
0

我正在尝试为每个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

1 回答

  • 1

    听起来你想要创建一个简单的散点图,每个ID有一个多个图表,反转Y轴 .

    如果要为每个ID创建一个包含多个图表的图表,可以使用ggplot的构面函数( facet_gridfacet_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
    

相关问题