问题
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.
回答1:
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)
回答2:
In response to question 1: The purpose of histograms is to display the density or frequency of continuous data. If you're trying to compare the mean / median / trimmed mean across the 3 categories in the row.name column, I suggest bar graphs. I'm not sure comparing mean / median / trimmed mean in a single graph is coherent to viewers, so it may be ideal to generate 3 bar graphs.
In response to question 2: If you aim to compare the 3 categories in the row.name column using multiple columns of data, I suggest a box-plot. I realize that the box-plot does not traditionally include the mean, but it is one of the best visualizations for comparing data across categories. Please see r-bloggers.com/box-plot-with-r-tutorial for an example.
来源:https://stackoverflow.com/questions/25583356/a-very-simple-histogram-with-r