一个非常简单的R直方图?

编程入门 行业动态 更新时间:2024-10-11 01:20:00
本文介绍了一个非常简单的R直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚开始使用R编程。我正在使用RStudio进行考试,并且必须以图形方式表示数据集上某些计算的结果。 我有这样的结构:

我想做的是用每行平均值的3个值制作一些直方图,中位数和修剪后的均值

第一个问题:这是图形表示此类数据的正确方法吗?还是有一些更好的图。

第二个问题:有人可以给我代码在x上绘制3个字符串的图形吗? ( Lobby, R& D, ROE),并在y轴上使用一个包含结果的值的比例尺,以使直方图代表在lobing中投资的差异,r&

希望我已经足够清楚了,如果我没有指定相关内容,请问我。

解决方案

听起来像您想要执行以下操作。将您的数据保存在具有以下格式的csv调用中 bar.csv :

部门平均中位数Trimmed_Mean 大厅0.008 0.0018 0.0058 R& D 6.25 3.2 4.78 ROE 19.08 16.66 16.276

您可以使用 library(ggplot2)和 library(reshape)以及此处列出的命令

dat.m <-read.csv( bar.csv) dat.m <-melt(dat.m,id.vars = Dept) ggplot(dat.m,aes(x = Dept,y = value,fill = variable))+ geom_bar(stat ='identity')+ facet_wrap(〜Dept,ncol = 3,scales = free_y)#facet包装 ggplot(dat.m,aes(x = Dept,y = value,fill = variable))+ geom_bar(stat ='identity')#堆积的条形

以显示以下图形:

正如zhaoy所说,一个直方图可以处理原始数据(通常),而您所拥有的就是摘要数据。另外,您可以使用 library(ggplot2)生成类似这样的箱线图摘要图(在ggplot2中使用 spray 数据库):

库(ggplot2) p <-qplot(spray,count,data = InsectSprays,geom = 'boxplot') p< -p + stat_summary(fun.y = mean,shape = 1,col ='red',geom ='point') print(p)

或者简单地使用标准的 boxplot 命令,并使用相同的数据,并带有显示平均值的功能:

boxplot(count〜spray,data = InsectSprays,col = lightgray)表示<-tapply(InsectSprays $ count,InsectSprays $ spray,mean)分(means,col = red,pch = 18)

I'm at very beginning with R programming. I'm using RStudio for an exam, and I have to represent graphically the results of some calculations on a dataset. I have a structure like that:

and what I was thinking to do was make some histograms with the 3 values of the mean for each row, and the same for median and trimmed mean.

First question: Is this a correct way to represent this kind of data graphically? Or there are some better plot.

Second question: Could someone give me the code to draw a graph with on the x avis the 3 strings ("Lobby", "R & D","ROE") and on the y axis a scale of values ​​that includes the results, in order to have the histograms representing the differences in investment in lobbing, r & d and the roe obtained.

Hope I've been clear enough, if I haven't specified something relevant please ask me.

解决方案

Its sounds like you want to do the following. With your data in a csv call bar.csv having this format:

Dept Mean Median Trimmed_Mean Lobby 0.008 0.0018 0.0058 R & D 6.25 3.2 4.78 ROE 19.08 16.66 16.276

You can use library(ggplot2) and library(reshape) and the commands listed here

dat.m<-read.csv("bar.csv") dat.m<-melt(dat.m,id.vars="Dept") ggplot(dat.m, aes(x = Dept, y = value,fill=variable)) + geom_bar(stat='identity')+ facet_wrap(~ Dept, ncol = 3,scales="free_y") #facet wrapped ggplot(dat.m, aes(x = Dept, y = value,fill=variable)) + geom_bar(stat='identity') #stacked bar

to display the graphs below:

As zhaoy says, a historgram works with raw data (usually) - and what you have is summary data. Also, you could use library(ggplot2) to produce a boxplot summary graph like this (using spray data in the ggplot2 library):

library(ggplot2) p<-qplot(spray,count,data=InsectSprays,geom='boxplot') p<-p+stat_summary(fun.y=mean,shape=1,col='red',geom='point') print(p)

Or simply using the standard boxplot command, with the same data, with added functionality to display the means:

boxplot(count ~ spray, data = InsectSprays, col = "lightgray") means <- tapply(InsectSprays$count,InsectSprays$spray,mean) points(means,col="red",pch=18)

更多推荐

一个非常简单的R直方图?

本文发布于:2023-11-10 07:13:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:直方图   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!