好吧,我几乎在那里,但仍有一两件事我想解决/理解。
我试图在geom_smooth()
的列data.table
上绘制一系列PlotCat
,用另一个变量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
关于几乎在那里...... 的传说(请参阅所需情节上的Control 0
和Control 1
); Control 0
和Control 1
的相同线型 - 这可能与{在levels
的{1}}中,我尝试添加更多内容以“推送”factor(PlotLine)
更高级别,但在图上没有明显的变化。任何提示?
答案 0 :(得分:1)
我不认为可以在不使用scale _ * _ manual的情况下组合两个音阶。但是,这是一个通用的解决方案,仅提供PlotCat的信息:
myDomain.com/escrits/41-advent