R Barplot调用导致错误:“'height'必须是向量或矩阵”

am46iovg  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(342)

我试着为这些数据制作一个条形图:

Area,Diversity
A,0.455
B,0.354
C,0.531
D,0.313

字符串
我尝试的代码是这样的:

barplot(data=pdiv, aes(x=Area, y=Diversity)) + theme_bw() + xlab("Area") + ylab("Simpson's Diversity score (1-D")


但它总是出现这样的错误:

Error in barplot.default(data = pdiv, aes(x = Area, y = Diversity)) : 
  'height' must be a vector or a matrix


接下来我可以尝试什么?

lp0sw83n

lp0sw83n1#

barplot()函数要求直接输入y轴值,而不是通过aes()函数。尝试,

Area <- c("A", "B", "C", "D")
Diversity <- c(0.455, 0.354, 0.531, 0.313)

pdiv <- data.frame(Area, Diversity)

barplot(
  height = pdiv$Diversity, 
  names.arg = pdiv$Area, 
  xlab = "Area", 
  ylab = "Simpson's Diversity score (1-D)",
  main = "Bar Chart"
)

字符串
现在barplot()函数中的height参数将y轴值作为向量。
测试结果:


的数据
或者使用等效的ggplot2版本:

ggplot(pdiv, aes(Area, Diversity)) + 
  geom_col() + 
  theme_bw() + 
  xlab("Area") + 
  ylab("Simpson's Diversity score (1-D)")


相关问题