简体   繁体   中英

ggplot2 Colour & Shape by different Factors

I have a data set with 2 factors (MACH & YOU) Id like to produce a BoxPlot using ggplot2 and have the BoxPlot colour split by MACH whilst highlighting certain points (YOU) in a different shape and in Black..?

I can get the plot working but i can't make the (YOU) factor be bigger in terms of shape and make it black...without effecting all other points on the graph. Ignore the commented lines - I was just playing around with those.

My dataframe x has the form

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") 

在此处输入图片说明

If you want to make the points for YOU of different size, depending on their value, you can add aes(size = factor(YOU)) inside geom_point() .

You can choose the range of size of the points adding scale_size_discrete(range = c(3, 6)) to you plot. In this example, the minimum size would be 3 and the maximum value would be 6.

That would be

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))

I would solve this by using two subsets and two calls to 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")

情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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