【发布时间】: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
我想解决/理解的问题
- 以某种方式在快到了...的图例上注明
linetype(参见Desired plot上的Control 0和Control 1); - 在Desired plot和差不多...上获得
Control 0和Control 1的相同线型 - 这可能与levels的levels有关factor(PlotLine),我尝试添加更多它们以“推动”"dashed"进入更高级别,但图上没有明显变化。
有什么提示吗?
【问题讨论】:
-
@jf328,它将遍历所有线型,我只想要两个 - 实线和虚线(并且能够轻松更改它们)。
-
1.我不知道你能不能把它们结合起来。但是,如果您不指定
guide=FALSE2,则可以使用另一个图例。您是对的。 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)获取与第一个图匹配的线型。这有点令人困惑,我会尝试更多地研究它。感谢您的宝贵时间!