Fixing the order of facets in ggplot

折月煮酒 提交于 2019-11-26 05:55:50

问题


Data:

type    size    amount  
T   50%     48.4    
F   50%     48.1    
P   50%     46.8    
T   100%    25.9    
F   100%    26.0    
P   100%    24.9    
T   150%    21.1    
F   150%    21.4    
P   150%    20.1    
T   200%    20.8    
F   200%    21.5    
P   200%    16.5

I need to plot a bargraph of the above data using ggplot (x-axis -> \"type\", y-axis -> \"amount\", group by \"size\"). When I used the following code, I am not getting the variable \"type\" and as well as \"size\" in the order shown in the data. Please see the figure. I have used the following code for that.

 ggplot(temp, aes(type, amount , fill=type, group=type, shape=type, facets=size)) + 
  geom_bar(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(.~size) + 
  theme_bw() + 
  scale_fill_manual(values = c(\"darkblue\",\"steelblue1\",\"steelblue4\"), 
                    labels = c(\"T\", \"F\", \"P\"))

\"enter .

For fixing the order issue, I have used a factor method for the variable \"type\" using the following. Please see the figure also.

temp$new = factor(temp$type, levels=c(\"T\",\"F\",\"P\"), labels=c(\"T\",\"F\",\"P\")) 

\"enter

However, now I don\'t know how to fix the order for the variable \"size\". It should be 50%, 100%. 150%, and 200%.


回答1:


Make your size a factor in your dataframe by:

temp$size_f = factor(temp$size, levels=c('50%','100%','150%','200%'))

Then change the facet_grid(.~size) to facet_grid(.~size_f)

Then plot:

The graphs are now in the correct order.




回答2:


Here's a solution that keeps things within a dplyr pipe chain. You sort the data in advance, and then using mutate_at to convert to a factor. I've modified the data slightly to show how this solution can be applied generally, given data that can be sensibly sorted:

# the data
temp <- data.frame(type=rep(c("T", "F", "P"), 4),
                    size=rep(c("50%", "100%", "200%", "150%"), each=3), # cannot sort this
                    size_num = rep(c(.5, 1, 2, 1.5), each=3), # can sort this
                    amount=c(48.4, 48.1, 46.8, 
                             25.9, 26.0, 24.9,
                             20.8, 21.5, 16.5,
                             21.1, 21.4, 20.1))

temp %>% 
  arrange(size_num) %>% # sort
  mutate_at(vars(size), funs(factor(., levels=unique(.)))) %>% # convert to factor

  ggplot() + 
  geom_bar(aes(x = type, y=amount, fill=type), 
           position="dodge", stat="identity") + 
  facet_grid(~ size)

You can apply this solution to arrange the bars within facets, too, though you can only choose a single, preferred order:

    temp %>% 
  arrange(size_num) %>%
  mutate_at(vars(size), funs(factor(., levels=unique(.)))) %>%
  arrange(desc(amount)) %>%
  mutate_at(vars(type), funs(factor(., levels=unique(.)))) %>%
  ggplot() + 
  geom_bar(aes(x = type, y=amount, fill=type), 
           position="dodge", stat="identity") + 
  facet_grid(~ size)


  ggplot() + 
  geom_bar(aes(x = type, y=amount, fill=type), 
           position="dodge", stat="identity") + 
  facet_grid(~ size)


来源:https://stackoverflow.com/questions/14262497/fixing-the-order-of-facets-in-ggplot

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