【问题标题】:ggplot2: Specifying a linetype with a column valueggplot2:用列值指定线型
【发布时间】:2016-04-22 10:54:05
【问题描述】:

问题/问题

嗯,我快到了,但还有一两件事我想解决/理解。

我正在尝试通过data.table 的列PlotCat 绘制一系列geom_smooth(),通过另一个变量PlotLine 指定两个不同的线型。

我正在努力寻找一个简洁的解决方案,在图例中可以看到线型。

设置

library(data.table)
library(ggplot2)
library(RColorBrewer)


dataT <- data.table(
  X = c(13.24, 22.8, 29.79, 32.13, 35.02, 40.8, 48.05, 59.89, 7.24, 18.33, 25.86, 29.74, 38.5, 48.5, 55.99, 6.71, 12.36, 22.8, 27.99, 41.11, 53.88, 61.34, 7.83, 23.85, 31.89, 37.38, 48.35, 59.62, 60.28, 5.8, 10.12, 18.12, 22.5, 32.98, 44.58, 51.39, 13.61, 19.41, 23.77, 27.5, 34.66, 39.63, 51.63, 1.06, 14.39, 20.2, 23.34, 31.56, 36.96, 50.94, 14.38, 26.85, 34.49, 39.33, 51.74, 60.11, 64.83),
  Y = c(0.02, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.02, 0, 0, 0.01, 0.01, 0.01, 0.01, 0.01, 0.02, 0.03, 0.03, 0.03, 0.03, 0.03, 0.04, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0, 0.02, 0.02, 0.02, 0.01, 0.02, 0.01, 0, 0, 0, 0, 0, 0, 0.01),
  PlotCat = c("Control 0", "Control 0", "Control 0", "Control 0", "Control 0", "Control 0", "Control 0", "Control 0", "Control 1", "Control 1", "Control 1", "Control 1", "Control 1", "Control 1", "Control 1", "Model Cat 1", "Model Cat 1", "Model Cat 1", "Model Cat 1", "Model Cat 1", "Model Cat 1", "Model Cat 1", "Model Cat 2", "Model Cat 2", "Model Cat 2", "Model Cat 2", "Model Cat 2", "Model Cat 2", "Model Cat 2", "Model Cat 3", "Model Cat 3", "Model Cat 3", "Model Cat 3", "Model Cat 3", "Model Cat 3", "Model Cat 3", "Model Cat 4", "Model Cat 4", "Model Cat 4", "Model Cat 4", "Model Cat 4", "Model Cat 4", "Model Cat 4", "Model Cat 5", "Model Cat 5", "Model Cat 5", "Model Cat 5", "Model Cat 5", "Model Cat 5", "Model Cat 5", "Model Cat 6", "Model Cat 6", "Model Cat 6", "Model Cat 6", "Model Cat 6", "Model Cat 6", "Model Cat 6"),
  PlotLine = c("dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "dashed", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid"))

# Sample of rows:
#         X    Y     PlotCat PlotLine
#  1: 13.24 0.02   Control 0   dashed
# 11: 25.86 0.01   Control 1   dashed
# 22: 61.34 0.04 Model Cat 1    solid
# 28: 59.62 0.02 Model Cat 2    solid
# 34: 32.98 0.01 Model Cat 3    solid
# 46: 20.20 0.02 Model Cat 5    solid

想要的情节

基于来自 this question 的已接受答案,但手动指定 linetype 值很难看。

g1 <- ggplot(data.frame(dataT),
             aes(x = X,
                 y = Y,
                 colour = PlotCat,
                 linetype = PlotCat)) +
  geom_point() +
  geom_smooth(level = 0.5, fill = NA) +
  theme_minimal() +
  coord_cartesian(ylim = c(0, 0.05)) +
  scale_linetype_manual(values = c(rep("dashed", dataT[PlotLine == "dashed", length(unique(PlotCat))]),
                                   rep("solid", dataT[PlotLine == "solid", length(unique(PlotCat))]))) + 
  ggtitle("Desired plot")
g1

在不手动指定值的情况下我能得到的最接近的值

dataT[, PlotLine := factor(PlotLine, levels = c("solid", "dashed"))]

g2 <- ggplot(data.frame(dataT),
             aes(x = X,
                 y = Y,
                 colour = PlotCat,
                 linetype = PlotLine)) +
  geom_point() +
  geom_smooth(level = 0.5, fill = NA) +
  theme_minimal() +
  coord_cartesian(ylim = c(0, 0.05)) +
  scale_linetype(guide = FALSE) + 
  ggtitle("Almost there...")
g2

我想解决/理解的问题

  1. 以某种方式在快到了...的图例上注明linetype(参见Desired plot上的Control 0Control 1);
  2. Desired plot差不多...上获得Control 0Control 1的相同线型 - 这可能与levelslevels有关factor(PlotLine),我尝试添加更多它们以“推动”"dashed" 进入更高级别,但图上没有明显变化。

有什么提示吗?

【问题讨论】:

  • @jf328,它将遍历所有线型,我只想要两个 - 实线和虚线(并且能够轻松更改它们)。
  • 1.我不知道你能不能把它们结合起来。但是,如果您不指定guide=FALSE 2,则可以使用另一个图例。您是对的。 ggplot 使用因子水平。您可以根据需要重新排序级别并获取所需的类型dataT[, PlotLine2 := reorder(PlotLine, PlotLine == 'dashed')]
  • @jf328, ad 1:我使用guide=FALSE 不打印两个图例,所以很明显,我正在寻找另一个答案,而不仅仅是取回它。广告 2:不幸的是,重新排序 PlotLine 给出了完全相同的情节。
  • 然后试试reorder(PlotLine, PlotLine == 'solid'),一个可以的:)
  • @jf328,看起来在这里使用因子不是答案,因为它仍然给出第二个图上显示的线型(即稍微窄一点的虚线),所以它不会改变任何东西 - 我仍然有使用scale_linetype_manual(values = c("solid", "dashed"), guide = FALSE) 获取与第一个图匹配的线型。这有点令人困惑,我会尝试更多地研究它。感谢您的宝贵时间!

标签: r ggplot2


【解决方案1】:

我认为在不使用 scale_*_manual 的情况下组合两个音阶是不可能的。但是,有一个通用的解决方案,只取消 PlotCat 的信息:

dataT$PlotCat = factor(dataT$PlotCat)

ggplot(data.frame(dataT), aes(x=X, y=Y, colour=PlotCat, linetype=PlotCat)) +
  geom_point() +
  geom_smooth(level = 0.5, fill = NA) +
  scale_linetype_manual(values=as.numeric(grepl("Control", levels(dataT$PlotCat)))+1)

【讨论】:

  • 这有点有限(想象一下为更多组编写那些ifelse...),但绝对比复制线型值更好,谢谢。让我们再等一下其他解决方案。
  • 您可以使用因子水平的数字表示(请参阅编辑后的解决方案)。这样它就与组的数量无关。
  • 如何将其他组添加到您的解决方案中?假设我们想用虚线绘制“Control 0”,用虚线绘制“Control 1”,用实线绘制所有“Model Cat X”。我已经将 sapply、grepl 和 max 一起“修补”了,但是解决方案又长又丑,而且肯定有比这更好的东西:dataT$PlotCat = factor(dataT$PlotCat); keys &lt;- c("Model", "Control 1", "Control 0"); cats &lt;- levels(dataT$PlotCat); linetypes &lt;- sapply(1:length(keys), function(i) { as.numeric(grepl(keys[i], cats, ignore.case=TRUE)) * i }) %&gt;% t() %&gt;% data.frame() %&gt;% summarise_each(funs(max(., na.rm = TRUE))) %&gt;% t()
猜你喜欢
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
相关资源
最近更新 更多