我刚开始使用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.276You 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 barto 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直方图?
发布评论