【问题标题】:How to change barplot in ggplot for R如何在 ggplot for R 中更改条形图
【发布时间】:2014-06-28 04:13:38
【问题描述】:

我在ggplot 中有一个情节,它向我展示了“银行”和“系统”中每个“评级”类别的价格水平。这是我的代码:

##fict
a<-c("rating1","rating2","rating3")
b<-c(1.2,1.2,1.3)
c<-c(1.6,1.4,1.6)

gg<-cbind('rating'=rep(a,2),'price'=c(b,c),'tipo'=rep(c("bank","sistem"),3))
gg<-as.data.frame(gg)


a<-rgb(red=150, green=191, blue=37, maxColorValue = 255)
b<-rgb(red=80, green=113, blue=14, maxColorValue = 255)


    ggplot(gg, aes(x=tipo, y=price,width=1)) +   
      geom_bar(position='stack', stat='identity', fill=c(b,a), color='black') +
      facet_wrap( ~ rating)+
      theme_bw() +  theme(axis.line = element_line(colour = "black"),
         panel.grid.major = element_blank(),
       panel.grid.minor = element_blank(),
       panel.border = element_blank(),
       panel.background = element_blank(),
    strip.background = element_rect(colour = 'white', fill = 'white', size = 3),
    axis.title.y=element_text(vjust=0.19),
    axis.title.x=element_text(vjust=0.19)
    #strip.text.x = element_text(colour = 'red', angle = 45, size = 10, hjust = 0.5, vjust = 0.5, face = 'bold')
    ) + xlab("My x label") +
      ylab("My y label") +
    labs(title = 'difference')

这段代码生成了我的情节。

我想改变 3 件事:

  • 我希望标签等级显示在底部
  • 我希望“银行”和“系统”标签消失,并使用带有银行和系统颜色的图例更改它。
  • 如果可能,也可以将图例水平放置在 x 轴标题下

谢谢

【问题讨论】:

  • 您可以在不分面的情况下完成大部分操作。试试ggplot(gg, aes(x=rating, y=price, fill=tipo)) + geom_bar(position='dodge', stat='identity', color='black')。您可以使用 themelegend.position 更改图例位置

标签: r plot ggplot2 lattice


【解决方案1】:

将评论升级为答案。

library(ggplot2)

# your data - tweaked the code - there is no need to cbind within data.frame
# and names do not need to be in quotes
gg <- data.frame(rating=rep(c("rating1","rating2","rating3"),2),
                 price=c(c(1.2,1.2,1.3),c(1.6,1.4,1.6)),
                 tipo=rep(c("bank","sistem"),3))

a <- rgb(red=150, green=191, blue=37, maxColorValue = 255)
b <- rgb(red=80, green=113, blue=14, maxColorValue = 255)

# Plot

# use position dodge to get the bars side-by-side
# theme_classic removes grid lines and uses theme_bw()
# scale_fill_manual to manually specify the colours - by using fill = tipo in the 
# aesthetic call of ggplot a legend will be generated
# scale_y_continuous - using expand starts the axis at exactly zero


ggplot(gg, aes(x=rating, y=price, fill=tipo)) +   
  geom_bar(position='dodge', stat='identity', color='black') +
  theme_classic() + 
  scale_fill_manual(values = c(b,a)) +
  scale_y_continuous(limit=c(0,2), expand=c(0,0)) +
  labs(title = 'difference', x = "My x label", y = "My y label") +
  theme(
      axis.title.y=element_text(vjust=0.19),
      axis.title.x=element_text(vjust=0.19) ,
      legend.position = "bottom",
      legend.title=element_blank())  

【讨论】:

  • 非常感谢您的帮助。另一个问题:有没有办法让图例没有斜线?
  • 欢迎您。由于在geom_bar 调用中添加了colour='black',因此图例会出现斜线。一个快速(且丑陋)的解决方法是添加第二个 geom_bar 调用,同时调整两者中的函数参数。见see bottom of the page in this post from the R cookbook
  • 谢谢!你现在有很好的 ggplot 教程吗?因为找那个我发现只有基本教程
  • 有几本书,但我认为ggplot2 help site 是一个不错的起点。此外,如果您知道您想要什么情节或想要调整情节的哪一部分,那么快速在线搜索几乎肯定会导致一个页面描述如何做到这一点。还可以查看在 SO 上提出的ggplot2 标签问题 - 总是有新的东西。我认为边做边学是一个很好的方法。
  • 你可能会发现一些有用的东西here
猜你喜欢
  • 1970-01-01
  • 2021-07-16
  • 2013-06-02
  • 1970-01-01
  • 2021-02-07
  • 2022-06-21
  • 1970-01-01
  • 2013-03-22
  • 2021-10-29
相关资源
最近更新 更多