繁体   English   中英

ggplot2颜色和形状受不同因素的影响

[英]ggplot2 Colour & Shape by different Factors

我有一个包含2个因子的数据集(MACH&YOU),我想使用ggplot2生成BoxPlot,并用MACH拆分BoxPlot颜色,同时以不同的形状和黑色突出显示某些点(YOU)。

我可以使绘图正常工作,但我不能使(YOU)因子在形状上变大并使它变黑...而不会影响图形上的所有其他点。 忽略注释行-我只是在玩那些。

我的数据框x具有以下形式

MEDIAN MACH     YOU        PROD
34.5   tool1    false      ME
33.8   tool1    false      ME
32.9   tool2    true       ME
30.1   tool2    true       ME
33.8   tool2    false.....etc

x<- data.frame(MEDIAN=c(34,32,56,34,45,34,45,33,23), MACH=c("t1","t1","t1","t2","t2","t2","t1","t1","t2"), YOU=c("false","false","false","false","true","true","true","false","false"), PROD="U","U","U","U","U","U","U","U","U")
ggplot(data=x,aes(MACH,MEDIAN ))+ 
    geom_boxplot(fill = "white", colour = "blue")+ 
    theme(panel.grid.minor = element_line(colour = "grey"), plot.title = element_text(size = rel(0.8)),axis.text.x = element_text(angle=90, vjust=1), strip.text.x = element_text(size = 8, colour = "black", face = "bold")) +  
    #geom_abline(colour = "grey80")+ 
    #geom_point(shape = factor(YOURLOTS)), size = 3) + 
    #geom_hline(yintercept=x$TARG_AVG,colour = "green")+ 
    #geom_hline(yintercept=x$TARG_MIN,colour = "red")+ 
    #geom_hline(yintercept=x$TARG_MAX,colour = "red")+ 
    geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0), aes(colour=factor(MACH),shape = factor(YOU)), size =3)+

    facet_wrap(~PROD, scales = "free") + 
    ggtitle("MyTitle") + 
    scale_size_area() + 
    xlab("STAGE HIST EQUIPID")+ 
    ylab("yaxis") 

在此处输入图片说明

如果要为您的点数不同,请根据其值,在geom_point()添加aes(size = factor(YOU)) geom_point()

您可以选择将scale_size_discrete(range = c(3, 6))到绘图中的点的大小scale_size_discrete(range = c(3, 6)) 在此示例中,最小大小为3,最大值为6。

那会是

ggplot(data = x, aes(MACH, MEDIAN)) + 
    geom_boxplot(fill = "white", aes(color = MACH)) + 
    geom_point(aes(shape = factor(YOU), size = factor(YOU)), color = "black", alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0)) + 
    labs(title = "My Title", x = "Stage Hist Equip ID", y = "y-axis") + 
    scale_size_discrete(range = c(3, 6))

我可以通过使用两个子集和两次对geom_point()调用来解决此问题:

library(ggplot2)

x <- data.frame(MEDIAN = c(34,32,56,34,45,34,45,33,23), 
                MACH   = c("t1","t1","t1","t2","t2","t2","t1","t1","t2"), 
                YOU    = c("false","false","false","false","true","true","true","false","false"), 
                PROD   = c("U","U","U","U","U","U","U","U","U"))

ggplot(data = x, aes(MACH, MEDIAN)) + 
  geom_boxplot(fill = "white", colour = "blue") + 
  geom_point(data = subset(x, YOU != "true"), aes(color = MACH), 
             size = 8, alpha = 0.6, 
             position = position_jitter(w = 0.05, h = 0.0)) +
  geom_point(data = subset(x, YOU == "true"), aes(shape = YOU), 
             color = "black", size = 8, alpha = 0.6,
             position = position_jitter(w = -0.05, h = 0.0)) +
  labs(title = "My Title", x = "Stage Hist Equip ID", y = "y-axis")

情节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM