ggplot2如何移动图例,以及如何填充和躲避多个变量?

时间:2023-01-05 19:05:02

Question 1: Is it possible to have ggplot fill by one variable, and dodge by another variable? In this example group I would like to group Claims within Categories? I have researched this site and tried playing around code by manipulating the variables used in the aes () code as well as the variables in the fill and dodge but to no avail. (Additional note, following a constructive comment on this: if you are not aware of a ggplot way to do this but would like recommend another graphics package you know can do this, I would be interested in the referral)

问题1:是否可以将ggplot填充一个变量,然后用另一个变量躲闪?在这个示例组中,我想将类别中的声明分组?我研究了这个网站,试图通过操纵aes()代码中使用的变量以及填充和闪避中的变量来玩代码,但无济于事。 (另外请注意,在对此进行建设性评论之后:如果你不知道ggplot的方法,但想要推荐另一个你知道可以做到这一点的图形包,我会对推荐感兴趣)

Question 2: What is the code to move the legend to the top of the graph. I know this must seem basic to some, but I have read many similar questions and the answers often seem out of date referring to the opts() function I have also tried using the theme and guides code written as comments below

问题2:将图例移动到图表顶部的代码是什么。我知道这对某些人来说似乎是基本的,但是我已经阅读了很多类似的问题,并且答案经常看起来过时,指的是opts()函数我也尝试过使用主题并指导代码编写为下面的注释

df<-data.frame (Categories=c("Alpha Category", "Alpha Category",  "Alpha Category", "Alpha Category", "Bravo Category",  "Bravo Category", "Bravo Category", "Charlie Category" ,  "Charlie Category", "Charlie Category"),  Claim=c("claim1", "claim2", "claim3", "claim4", "claim5", "claim6", "claim7", "claim8","claim9", "claim10")  , Strong.Agreement=c(66,57,59,84,77,78,54,53,70,67), Somewhat.Agreement=c(18, 32, 35, 13, 22, 18, 36, 38, 27, 32) )
df$Claim <- reorder(df$Claim,df$Strong.Agreement)      
df.melt = melt(df, id.vars=c("Categories", "Claim"), measure.vars=c("Strong.Agreement","Somewhat.Agreement"), variable.name="Agreement", value.name="Ratings")  # Melt the data frame   
agreement.plot <- ggplot(df.melt, aes(x = Claim, y = Ratings, fill = Agreement)) +              
  geom_bar(stat="identity") + coord_flip()+
 theme_classic(base_size = 15, base_family = "") + scale_fill_brewer(palette="Paired")
 #+ theme(legend.position = "top")
  #guides(colour = guide_legend(title.position = "top"))
agreement.plot

1 个解决方案

#1


1  

This would be my approach:

这将是我的方法:

ggplot(df.melt, aes(x = Claim, y = Ratings, fill = Agreement)) +              
    geom_bar(stat="identity") + coord_flip()+
    theme_classic(base_size = 15, base_family = "") + 
    scale_fill_brewer(palette="Paired") +
    theme(legend.position="top") +
    facet_grid(Categories~.)

I use facet_grid instead of the dodge and fill. Not sure how to do both dodge and fill or if it's possible.

我使用facet_grid代替闪避和填充。不确定如何同时做躲闪和填充,或者是否可能。

#1


1  

This would be my approach:

这将是我的方法:

ggplot(df.melt, aes(x = Claim, y = Ratings, fill = Agreement)) +              
    geom_bar(stat="identity") + coord_flip()+
    theme_classic(base_size = 15, base_family = "") + 
    scale_fill_brewer(palette="Paired") +
    theme(legend.position="top") +
    facet_grid(Categories~.)

I use facet_grid instead of the dodge and fill. Not sure how to do both dodge and fill or if it's possible.

我使用facet_grid代替闪避和填充。不确定如何同时做躲闪和填充,或者是否可能。