【问题标题】:ggplot2 and function for several plots with same variablesggplot2 和函数用于具有相同变量的多个绘图
【发布时间】:2013-11-19 14:38:06
【问题描述】:

我想绘制 10 个数字,具有相同的变量,但不同的演员(我想为每个演员绘制一个情节)。

actor1 <- subset (actor, actor$Actor=="Actor1")

plot_actor1 <- ggplot(actor1, aes(x = Date, y = Var))+
  geom_point(shape=15)
  ylim(0,30)+
  geom_hline(yintercept=12)+
  geom_hline(yintercept=17)+
  theme_bw()

pdf("actor1", paper='A4r', width=11)
actor1
dev.off()

数据(“演员” - 摘录):

Actor   Date    Var
Actor1  0101    12
Actor1  0102    14
Actor1  0103    17
Actor1  0104    11
Actor1  0105    9
Actor1  0106    21
Actor2  0101    6
Actor2  0102    17
Actor2  0103    15
Actor2  0104    19
Actor2  0105    25
Actor2  0106    3

我可以手动执行此操作,但是否有其他解决方案(使用循环或函数),我可以将“actor1”替换为所有演员的名称(从 actor1 到 actor10)?

【问题讨论】:

  • 看看facet_grid()
  • 昨天我回答了a similar question关于创建和保存几个图,循环分组变量。您可以检查是否可以根据您的特定需求调整答案。我知道在 SO 上还有其他几个很好的例子。干杯。
  • 谢谢。效果很好!!!

标签: r loops ggplot2


【解决方案1】:

如果我理解正确的话,你应该可以添加

+ facet_grid(.~Actor)

到 ggplot 调用。

【讨论】:

  • 没有。我不想要一个有 10 个方面的情节,而是 10 个情节。
猜你喜欢
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多