用 geom_point 和 ggtern 中的黑色轮廓填充
Fill by geom_point and black outline in a ggtern
如何使用选定的填充 red/blue 和黑色轮廓为 A 和 D 上色。检查我的 看看我尝试了什么。谢谢!
obs <- c("Gene1", "Gene2", "Gene3", "Gene4","Gene5", "Gene6")
func1 <- c("A", "B", "C", "D", "C", "A")
func2 <- c("A1", "B1", "C1", "D1", "C2", "A2")
Cond1 <- c(0.007623561, 0.004639893, 0.000994121, 0.017494429, 0.000366445, 0.006663334)
Cond2 <- c(0.011299941, 0.009994388, 0.001012428, 0.013695669, 0.000299771, 0.010287904)
Cond3 <- c(0.005055458, 0.016826251, 0.001311254, 0.016115009, 0.000242897, 0.004583889)
df <- data.frame(obs, func1, func2, Cond1, Cond2, Cond3)
library(ggtern)
g = ggtern(data=df,aes(x=Cond1,y=Cond2,z=Cond3)) +
theme_bw() +
geom_point(aes(fill = c("A" = "red", "D" = "blue"), shape=21, colour="black" ) +
labs(x="Cond1",y="Cond2",z="Cond3") +
scale_T_continuous(breaks=unique(df$x))+
scale_L_continuous(breaks=unique(df$y))+
scale_R_continuous(breaks=unique(df$z))
期望的输出:
您需要创建一个命名的颜色列表,然后使用 scale_fill_manual
函数。
#A General way of creating a name list of colors
# col<-c("A" = "red", "B"="black", "C"="black", "D"="blue")
col<-rep("black", length(unique(df$func1)))
names(col) <- unique(df$func1)
col[which(names(col)=="A")] <- 'red'
col[which(names(col)=="D")] <- 'blue'
library(ggtern)
g <- ggtern(data=df, aes(x=Cond1,y=Cond2,z=Cond3)) +
theme_bw() +
geom_point(aes(fill=func1), shape=21, colour="black" ) +
scale_fill_manual(values=col) +
labs(x="Cond1",y="Cond2",z="Cond3") +
scale_T_continuous(breaks=unique(df$x))+
scale_L_continuous(breaks=unique(df$y))+
scale_R_continuous(breaks=unique(df$z))
print(g)
更新
ggplot 按定义的顺序绘制不同的层。为了在其他点之上绘制 A 和 D 点,可以在原始定义之后添加另一个 geom_point 定义。
在这里我创建了 A&D 点的子集并将它们绘制在初始集上。
df_ad <- df[(df$func1=="A" | df$func1=="D"),]
ggtern(data=df, aes(x=Cond1,y=Cond2,z=Cond3)) +
theme_bw() +
geom_point(aes(fill=func1), shape=21, colour="black" ) +
geom_point(data=df_ad, aes(x=Cond1,y=Cond2,z=Cond3, fill=func1), shape=21) +
scale_fill_manual(values=col) #+ ....
如何使用选定的填充 red/blue 和黑色轮廓为 A 和 D 上色。检查我的
obs <- c("Gene1", "Gene2", "Gene3", "Gene4","Gene5", "Gene6")
func1 <- c("A", "B", "C", "D", "C", "A")
func2 <- c("A1", "B1", "C1", "D1", "C2", "A2")
Cond1 <- c(0.007623561, 0.004639893, 0.000994121, 0.017494429, 0.000366445, 0.006663334)
Cond2 <- c(0.011299941, 0.009994388, 0.001012428, 0.013695669, 0.000299771, 0.010287904)
Cond3 <- c(0.005055458, 0.016826251, 0.001311254, 0.016115009, 0.000242897, 0.004583889)
df <- data.frame(obs, func1, func2, Cond1, Cond2, Cond3)
library(ggtern)
g = ggtern(data=df,aes(x=Cond1,y=Cond2,z=Cond3)) +
theme_bw() +
geom_point(aes(fill = c("A" = "red", "D" = "blue"), shape=21, colour="black" ) +
labs(x="Cond1",y="Cond2",z="Cond3") +
scale_T_continuous(breaks=unique(df$x))+
scale_L_continuous(breaks=unique(df$y))+
scale_R_continuous(breaks=unique(df$z))
期望的输出:
您需要创建一个命名的颜色列表,然后使用 scale_fill_manual
函数。
#A General way of creating a name list of colors
# col<-c("A" = "red", "B"="black", "C"="black", "D"="blue")
col<-rep("black", length(unique(df$func1)))
names(col) <- unique(df$func1)
col[which(names(col)=="A")] <- 'red'
col[which(names(col)=="D")] <- 'blue'
library(ggtern)
g <- ggtern(data=df, aes(x=Cond1,y=Cond2,z=Cond3)) +
theme_bw() +
geom_point(aes(fill=func1), shape=21, colour="black" ) +
scale_fill_manual(values=col) +
labs(x="Cond1",y="Cond2",z="Cond3") +
scale_T_continuous(breaks=unique(df$x))+
scale_L_continuous(breaks=unique(df$y))+
scale_R_continuous(breaks=unique(df$z))
print(g)
更新
ggplot 按定义的顺序绘制不同的层。为了在其他点之上绘制 A 和 D 点,可以在原始定义之后添加另一个 geom_point 定义。
在这里我创建了 A&D 点的子集并将它们绘制在初始集上。
df_ad <- df[(df$func1=="A" | df$func1=="D"),]
ggtern(data=df, aes(x=Cond1,y=Cond2,z=Cond3)) +
theme_bw() +
geom_point(aes(fill=func1), shape=21, colour="black" ) +
geom_point(data=df_ad, aes(x=Cond1,y=Cond2,z=Cond3, fill=func1), shape=21) +
scale_fill_manual(values=col) #+ ....