How to reverse legend (labels and color) so high value starts downstairs?

[亡魂溺海] 提交于 2019-12-04 15:59:09

问题


whats the best way to invert the legend label order so the 7 is down and the 1 is upstairs?

df$day <- as.numeric(df3$day)
blues <- colorRampPalette(c('#132B43', '#56B1F7'))

p4 <- 
    ggplot(subset(df,feedback==1&stp>20), aes(x=correct, fill=day, colour=day)) +
    geom_histogram(colour="black", binwidth=10) +
    facet_grid(day ~ .) +
    ggtitle("Over-pronation histogram") +
    ylab("Count (150s period)") +
    xlab("% Steps in over-pronation") +guide_legend(reverse = false)

回答1:


Your code is quite strange, with false instead of FALSE and incorrectly placed guide_legend. The correct usage is (@Harpal gives a hint on that):

ggplot(data.frame(x=1:4, y=4:1, col=factor(1:4)), aes(x=x, y=y, col=col)) + 
  geom_point(size=10)
ggplot(data.frame(x=1:4, y=4:1, col=factor(1:4)), aes(x=x, y=y, col=col)) + 
  geom_point(size=10) + guides(colour = guide_legend(reverse=T))




回答2:


If you're putting it in numeric and it's a continuous scale, you're better off with scale_fill_continuous(trans = 'reverse') or scale_colour_continuous. Using your code, this would give:

ggplot(subset(df,feedback==1&stp>20), aes(x=correct, fill=day, colour=day)) +
    geom_histogram(colour="black", binwidth=10) +
    facet_grid(day ~ .) +
    ggtitle("Over-pronation histogram") +
    ylab("Count (150s period)") +
    xlab("% Steps in over-pronation")+
    scale_fill_continuous(trans = 'reverse')



回答3:


For continuous scales, guide_colorbar is required.

Here I reverse color direction. Then I reverse color and size order with different functions

library(tidyverse)
library(janitor)
iris %>% 
  as_tibble() %>% 
  clean_names() %>% 
  ggplot(aes(x = sepal_length,
             y = petal_width,
             size = sepal_width,
             color = petal_length)) +
  geom_point() +
  facet_wrap(~species,scales = "free") +
  #reverse color direction (the higher in value, the darker in color)
  scale_color_continuous(trans = 'reverse') +
  #edit legends
  guides(
    #reverse color order (higher value on top)
    color = guide_colorbar(reverse = TRUE),
    #reverse size order (higher diameter on top) 
    size = guide_legend(reverse = TRUE))


来源:https://stackoverflow.com/questions/22458970/how-to-reverse-legend-labels-and-color-so-high-value-starts-downstairs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!