简体   繁体   中英

Remove variables from box plot legend ggplot2

I have a box and whisker plot, but I can't figure out how to remove certain variables from the legend.

This is the code I'm using

number_ticks <- function(n) {function(limits) pretty(limits, n)}
plot<-ggplot(data,aes(x=Group,y=Hue,fill=Group))+
  geom_boxplot()
plot + scale_y_continuous(breaks=number_ticks(10.5)) +scale_fill_manual(values = c("Control Day" = "red",
                                                                                 "Control Night" = "lightblue",
                                                                                 "Experimental Day" = "red",
                                                                                 "Experimental Night" = "lightblue"), labels=c("Day", "Night")) + coord_flip()

and this is the plot I'm getting: 箱形图

How can I remove the 'NA' variables from the legend?

Thanks!

ETA:

I only want 'Day' and 'Night' in my legend! The NA's are labelled as such intentionally.

I think you can use separate from tidyr package to create a new column for Day/Night and Experimental/Control cases.

data %>%  separate(Group , sep =" " , remove = FALSE , c("Condition_1" , "Condition_2")) %>% 
ggplot(aes(x=Group,y=Hue,fill=Condition_2))+ 
geom_boxplot() + scale_y_continuous(breaks=number_ticks(10.5))+
scale_fill_manual(values = c("red", "lightblue"), labels=c("Day", "Night")) + coord_flip()

With separate I have created two new columns, Condition_1 for Experimental/Control and Condition_2 for Day/Night and used Condition_2 for filling the boxes.

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