问题 1:是否可以让 ggplot 填充一个变量,并躲避另一个变量?在这个示例组中,我想在类别中对声明进行分组?我研究了这个站点并尝试通过操纵 aes() 代码中使用的变量以及填充和闪避中的变量来玩弄代码,但无济于事。(附加说明,在对此提出建设性评论后:如果您不知道 ggplot 方法可以做到这一点,但想推荐另一个您知道可以做到这一点的图形包,我会对推荐感兴趣)
问题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