对于示例数据框:
df <-structure(list(Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"),
RSL = c(-1.84, 7.68, 18.4, 13, 39.8, 98.4, 129.9, 94.9, 138,
142, 10, 50, 60, 80), Age = c(1181.5, 4633, 5075.5, 5741.5,
8430.5, 9784, 10095, 10366, 10095, 10095, 6000, 13500, 13000,
12000)), .Names = c("Type", "RSL", "Age"), class = "data.frame", row.names = c(NA,
-14L))
我想生成一个年龄相对于RSL绘制的图形(这是一个单独的图形,它构成了许多多面图形的一部分)。
library(ggplot2)
library("reshape2")
g <- ggplot (df, aes(x=Age, y=RSL, shape = Type, colour=Type)) +
geom_point(size=1.5) +
scale_shape_manual(values=c(19,19,19,19,19))+ # sets shape of points
scale_colour_manual(values=c("red", "black", "blue")) #sets colour of points
g
但不是A,B和C都是积分,我希望C是一条直线(没有点)。
如何处理这个问题的最佳方法?我相信我需要融化我的数据:
df_long <- melt(df, id="Type")
...但是无法弄清楚如何组装我的图表?
答案 0 :(得分:3)
这也应该有效,图例也会显示所有3个:
ggplot () +
geom_point(data = df, aes(x=Age, y=RSL, colour=Type), size=0, alpha = 0)+
geom_point(data = df[df$Type!="C",], aes(x=Age, y=RSL, colour = Type), size=1.5) +
geom_line(data = df[df$Type=="C",], aes(x=Age, y=RSL), size=0.7, color="blue")+
scale_colour_manual(values=c("red", "black", "blue"))
答案 1 :(得分:2)
使用C
将数据与geom
传递到不同的geom_line
图层(在您的情况下为data = subset(df, Type == "C"
)。
library(ggplot2)
ggplot(subset(df, Type != "C"), aes(Age, RSL)) +
geom_point(aes(shape = Type, colour = Type), size = 1.5) +
geom_line(data = subset(df, Type == "C"), color = "blue") +
scale_shape_manual(values=c(19,19,19,19,19))+
scale_colour_manual(values = c("red", "black"))