【问题标题】:Y axis doesn't show label name in RY 轴在 R 中不显示标签名称
【发布时间】:2014-10-05 22:36:58
【问题描述】:

我刚开始使用 R ,我使用 RStudio。

现在我正在玩情节只是为了看看它们是如何工作的。

这是我的数据:

https://www.dropbox.com/s/wp4gs8qamdo1irk/State_Zhvi_Summary_AllHomes.csv?dl=0

我一直在尝试针对 $RegionName 为 $Zhvi 绘制条形图

这是我的代码:

labels <- allHomet$RegionName
mp<-barplot(round((allHomet$Zhvi)/1000,digits=2)[order(labels)],horiz=TRUE,las=1,col=c(palette(heat.colors(6,alpha=1))),
        border=NA,
        main="Price of housing in United states",
        xlab="Price",
        las=2 )!

here is the plot I get

我没有在 y 轴上获得区域名称,它显示为空白。

谁能告诉我如何让区域名称出现?

【问题讨论】:

  • 看看'?barplot'中的'names.arg'
  • 是的,它填充了名称,但它搞砸了,这里是情节https://www.dropbox.com/s/g7fxxbzy50bhhsh/Rplot01.png?dl=0
  • 尝试使用“png”(或“jpeg”、“pdf”等)函数导出绘图并调整尺寸(参数“hight”和“width”)。设置较低的“cex”也可能有所帮助。

标签: r plot rstudio


【解决方案1】:

这是 ggplot2 非常适合处理的问题。它的灵活性和模块化使您可以轻松地立即生成这样的图表:

library(ggplot2) # load graphics package
head(df)
fl <- as.character(rep(1:6, length.out = nrow(df))) # fill variable - anything here
ggplot(df) + 
  geom_bar(aes(x = RegionName, y = Zhvi, fill = fl), stat = "identity", position = "dodge") + 
  scale_fill_brewer(type = "qual", name = "") +  # customise colours and legend title
  coord_flip() # make bars horizontal

【讨论】:

  • 我试过了,它给了我这个错误:错误:将变量映射到 y 并使用 stat="bin"。使用 stat="bin",它将尝试将 y 值设置为每组中的案例数。这可能会导致意外行为,并且不会在未来版本的 ggplot2 中被允许。如果您希望 y 表示案例计数,请使用 stat="bin" 并且不要将变量映射到 y。如果您希望 y 表示数据中的值,请使用 stat="identity"。有关示例,请参见 ?geom_bar。 (已失效;最后在 0.9.2 版本中使用)
  • 啊哈 - ggplot2 最近跳到了 1.0 版,导致上面的代码失败:cran.r-project.org/web/packages/ggplot2/vignettes/release.html。简单的解决方案:在fill = fl), 之后添加stat = "identity",。我已更新答案以包含此内容。
猜你喜欢
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
相关资源
最近更新 更多