在R中绘制条形图

用户名

这是数据快照: 在此处输入图片说明

restaurant_change_sales = c(3330.443, 3122.534)
restaurant_change_labor = c(696.592, 624.841)
restaurant_change_POS = c(155.48, 139.27)
rest_change = data.frame(restaurant_change_sales, restaurant_change_labor, restaurant_change_POS)

我希望每个列都有两个指示变化的栏。每个列一个图形。

我试过了:

ggplot(aes(x = rest_change$restaurant_change_sales), data = rest_change) + geom_bar()

这并没有给我想要的结果。请帮忙!!

常识

您的数据格式不正确,无法与ggplot2或R中的任何绘图软件包一起正常使用。因此,我们将首先修复数据,然后使用ggplot2对其进行绘图。

library(tidyr)
library(dplyr)
library(ggplot2)

# We need to differentiate between the values in the rows for them to make sense.
rest_change$category <- c('first val', 'second val')

# Now we use tidyr to reshape the data to the format that ggplot2 expects.
rc2 <- rest_change %>% gather(variable, value, -category)
rc2

# Now we can plot it.
# The category that we added goes along the x-axis, the values go along the y-axis.
# We want a bar chart and the value column contains absolute values, so no summation
# necessary, hence we use 'identity'.
# facet_grid() gives three miniplots within the image for each of the variables.
ggplot2(rc2, aes(x=category, y=value, facet=variable)) +
    geom_bar(stat='identity') +
    facet_grid(~variable)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章