繁体   English   中英

在x轴上具有多个变量的图1直方图

[英]Plot 1 histogram chart with multiple variables on the x axis

我想在1个图表中显示多个变量。 它是多个类别的数字总和。 数据集为total_sales

Sum_Clothes Sum_Shoes Sum_Bags  Sum_Belts  Sum_Glasses
101456         56709    152908    40670      97654

我已经尝试使用ggplot创建这个

ggplot(total_sales, aes(x=c("Sum_Clothes", "Sum_Shoes", "Sum_Bags", "Sum_Belts", "Sum_Glasses"))) + 
geom_histogram(bin=10) + scale_x_log10()

我预期会在x轴上显示所有变量并在直方图中显示数量,但出现“意外错误”

如果我已正确理解您的要求,那么您所要要做的就是在带有对数y轴的条形图中绘制这五个值。

首先,您需要转置数据框。

total_sales <- t(total_sales)

#If you're not working with a data.frame, instead run this and skip to the plot-section:

total_sales <- data.frame(sums = names(total_sales), total_sales)

接下来重命名您的列名

colnames(total_sales) <- c("sums", "sales")

最后,使用标准ggplot2-函数进行绘图

ggplot(total_sales, aes(x=sums, y=sales), fill=sums) + 
  geom_histogram(stat = 'identity') + scale_y_continuous(trans="log10")

编辑:添加了填充参数,以便每个总和的颜色应不同。 我怀疑由于总和的相似性,您收到了暗灰色的图。

bin参数表示您希望条形图有多少条。 在给定的数据中,您需要5个容器才能正确表示数据(每个总和1个容器)。 由于有5种不同的总和,因此ggplot会自动将bin的数量设置为5。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM