在R中的数据框中每个数值的绘制平均值和标准偏差
我想绘制每个数字列的均值为条,标准偏差是通过栏的线。如何为iris
数据集执行此操作?
我正在尝试更改数据集,以使其易于在GGPLOT2中绘制。
What I've tried
iris %>%
dplyr::select_if(is.numeric) %>%
dplyr::summarise(avg_sepal_length = mean(Sepal.Length),
avg_sepal_width = mean(Sepal.Width),
avg_petal_length = mean(Petal.Length),
avg_petal_width = mean(Petal.Width),
sd_sepal_length = sd(Sepal.Length),
sd_sepal_width = sd(Sepal.Width),
sd_petal_length = sd(Petal.Length),
sd_petal_width = sd(Petal.Width))
I want to pivot into two columns so the dataframe will look like so:
stat mean sd
sepal_length 5.843333 0.8280661
sepal_width 3.057333 0.4358663
petal_length 3.758 1.765298
pedal_width 1.199333 0.7622377
And then plot the upperbound and lower bound as a line for the sd and the.均值作为GGPLOT中的酒吧
I want to plot every numeric column with the mean as a bar and the standard deviation is a line through the bar. How can I do this for the iris
dataset?
I'm trying to transform my dataset to make it easy to plot in ggplot2.
What I've tried
iris %>%
dplyr::select_if(is.numeric) %>%
dplyr::summarise(avg_sepal_length = mean(Sepal.Length),
avg_sepal_width = mean(Sepal.Width),
avg_petal_length = mean(Petal.Length),
avg_petal_width = mean(Petal.Width),
sd_sepal_length = sd(Sepal.Length),
sd_sepal_width = sd(Sepal.Width),
sd_petal_length = sd(Petal.Length),
sd_petal_width = sd(Petal.Width))
I want to pivot into two columns so the dataframe will look like so:
stat mean sd
sepal_length 5.843333 0.8280661
sepal_width 3.057333 0.4358663
petal_length 3.758 1.765298
pedal_width 1.199333 0.7622377
And then plot the upperbound and lower bound as a line for the sd and the. mean as a bar in ggplot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了达到所需的结果,您可以首先使用
dplyr ::跨
简化代码。 Afterwards you could convert to long viapivot_longer
whereby using the.value
allows to put themean
s and thesd
s在他们自己的列中。 Finally you could make your plot as a combination of eggeom_col
andgeom_pointrange
:To achieve your desired result you could first simplify your code using
dplyr::across
. Afterwards you could convert to long viapivot_longer
whereby using the.value
allows to put themean
s and thesd
s in their own columns. Finally you could make your plot as a combination of e.g.geom_col
andgeom_pointrange
:您的输出格式不是
ggplot2
的最佳格式,它更喜欢它的时间更长:由于您熟悉
iris
数据集,因此值得检查 docs for 大量使用它。要达到您的格式,您可以将以下内容添加到管道:
Your output format is not the best format for
ggplot2
, which prefers it even longer:As you are familiar with the
iris
dataset, it is worth checking out the docs foracross
which make heavy use of it.To get to your format you can add the following to the pipe:
请注意,您实际上不需要预处理DF即可计算摘要值,您可以直接使用GGPLOT2的
stat_summary
:在这里,我已经使用了Base R的Simple R的Simple
stack
使虹膜数据集的长版本的功能;您可以使用自己喜欢的任何库(尤其是要包括其他操纵的情况下)。Note that you don't actually need to pre-process the df to calculate the summary values, you can use ggplot2's
stat_summary
directly:Here I've used base R's simple
stack
function to make a long version of the iris dataset; you can use whatever libraries you prefer (especially if you want to include other manipulations).您只需尝试
You can simply try