简体   繁体   English

如何在箱线图上打印频率-ggplot-R

[英]How to print frequency on boxplot - ggplot - R

This is my Dataframe 这是我的数据框

X color   cut      carrat
1   E    Ideal     0.23
2   J    Premiun   0.34
3   E    Ideal     0.23
...

I want to create a box plot of my column var "cut". 我想创建我的列var“ cut”的箱形图。

Here is the code for what I want to do... 这是我想做的代码...

ggplot(diamonds, aes(cut)) + geom_boxplot()

However, it isn't working properly as it's asking that aesthetics var "y", is missing. 但是,它无法正常运行,因为它要求缺少美学变量“ y”。

It's because I am not providing a second var to aes, what's the second var I have to supply? 这是因为我没有向es提供第二个变种,我必须提供的第二个变种是什么? Because I only need in the x's edge the different values my var "cut" can takes and in the Y's edge the frequendy of it. 因为我只需要x边缘的var“ cut”可以取的不同值,而Y边缘的频率就可以。

Thanks. 谢谢。

The below code will do the job: 下面的代码将完成这项工作:

library(ggplot2)
p <- ggplot(diamonds, aes(x = cut, y=carat))
p <- p + geom_boxplot(outlier.colour = "blue", fill="grey85")
p <- p + labs(title = "Diamonds carats")
show(p)

a boxplot is a grouping of some data distributions, therefore you must provide the data whose distribution you want to boxplot. 箱线图是一些数据分布的分组,因此,您必须提供要对其箱分布进行分布的数据。 The x aesthetics must represent the type, the y must represent the data set. x美学必须代表类型, y必须代表数据集。

在此处输入图片说明

A boxplot needs ay variable because it is illustrating a distribution. 箱线图需要ay变量,因为它在说明分布。 The count of rows by cut will not be a distribution but just one number per cut. 剪切的行数不是分布,而是每个剪切只有一个。 Here are the counts of each cut: 以下是每次切割的次数:

> table(diamonds$cut)

     Fair      Good Very Good   Premium     Ideal 
     1610      4906     12082     13791     21551

That information can not be shown in a box plot. 该信息无法在箱形图中显示。 Instead, you could use a bar chart like so: 相反,您可以使用如下所示的条形图:

ggplot(diamonds, aes(x = cut)) +
  geom_bar()

Which produces this chart: 产生此图的图表:

条形图

Or here is the code of a boxplot of cut and carat, as an example: 或者下面是切割和克拉的箱形图的代码,例如:

ggplot(diamonds, aes(x = cut, y = carat)) +
  geom_boxplot()

And the chart: 和图表:

箱形图

So the boxplot is showing the mean, range and quartiles of the carat column by each level of cut. 因此,箱图显示了按切割级别划分的克拉列的均值,范围和四分位数。

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

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