[英]r grouped barplot from Excel CSV file
I'm trying to make a grouped barplot in r, but there are some things I cannot figure out. 我正试图在r中制作一个分组的条形图,但有一些我无法弄清楚的事情。 This is what I have so far:
这是我到目前为止:
I would like: 我想要:
I used the following code: 我使用了以下代码:
x<-matrix(nrow=3,ncol=7, data=c(200,227,196,210,279,319,220,126,111,230,196,123,240,106,94,250,154,233,260,226,218))
tiff("p_month_all.tiff", width=600, height=300)
par(mar=c(5,4,0.5,0.5))
a=c("November","December","January","February","March","April","May")
barplot(x, beside=TRUE, ylim=c(0,350),xlab="Month", axes=TRUE,axis.lty=1, ylab="Monthly Precipitation [mm]", col=c("darkblue","dodgerblue3","deepskyblue1"),panel.first= abline(h = c(50,100,150,200,250,300), col = "grey", lty = 2), xaxt="n", yaxt="n")
par(ps=12, cex =1, cex.main=2)
axis(2, c(0,350, c(50, 100, 150, 200, 250, 300)), las=1)
dev.off()
The data set (.csv file) looks like this: 数据集(.csv文件)如下所示:
Month Hornberg Strick Huetten
November 120 278 234
December 279 156 145
January 328 300 299
February 267 259 234
March 190 201 187
April 150 199 177
May 147 156 160
I've rewritten your code for clarity so you can see more easily what the problem is. 为了清晰起见,我重写了您的代码,以便您可以更轻松地查看问题所在。
You were suppressing the axes with xaxt = "n"
and yaxt = "n"
. 你用
xaxt = "n"
和yaxt = "n"
来抑制轴。 I removed those lines. 我删除了这些行。 Adding a call to
box
draws the box around the plot. 添加对
box
的调用会在绘图周围绘制框。 Adding a call to grid
draws gridlines in the plot. 添加对
grid
的调用会在绘图中绘制网格线。 I've added row and column names to your data matrix so the plot know what to use in the axes. 我已经在您的数据矩阵中添加了行名和列名,因此绘图知道了在轴中使用的内容。 I've updated the plot margins.
我更新了情节边距。 I also tidied a few bits like replacing month names with
month.name
and using seq.int
rather than a hard-coded sequence. 我还整理了一些比如用
month.name
替换月份名称并使用seq.int
而不是硬编码序列。
x <- matrix(
c(
200, 227, 196,
210, 279, 319,
220, 126, 111,
230, 196, 123,
240, 106, 94,
250, 154, 233,
260, 226, 218
),
nrow = 3,
ncol = 7
)
colnames(x) <- month.name[c(11:12, 1:5)]
rownames(x) <- c("Hornberg", "Strick", "Huetten")
par(mar = c(5, 4, 1.5, 0.5), ps = 12, cex = 1, cex.main = 2, las = 1)
barplot(
x,
beside = TRUE,
ylim = c(0,350),
xlab = "Month",
axes = TRUE,
axis.lty = 1,
ylab = "Monthly Precipitation [mm]",
col = c("darkblue", "dodgerblue3", "deepskyblue1"),
panel.first = abline(
h = seq.int(50, 300, 50),
col = "grey",
lty = 2
)
)
box()
grid()
So, first of all, look through ggplot2 documentation, it's pretty good http://docs.ggplot2.org/0.9.3.1/index.html If you haven't found an answer for your question, never give up googling :) 所以,首先,看看ggplot2文档,它是相当不错的http://docs.ggplot2.org/0.9.3.1/index.html如果你还没有找到你的问题的答案,永远不要放弃谷歌搜索:)
Ok, about your question: 好的,关于你的问题:
help(read.csv) -> import your data to data.frame named x Prepare data for the plot: help(read.csv) - >将数据导入到名为x的data.frame中为图准备数据:
Melt your data to use it for the plot 融化您的数据以将其用于绘图
x<-melt(x)
Use Month variable as a factor and order by month: 使用Month变量作为因子并按月排序:
x$Month=factor(x$Month,level=month.name) x<-x[order(x$Month),]
Plot the graph using ggplot2 (as you tagged it here and it's straitforward in use) 使用ggplot2绘制图形(当你在这里标记它并且它在使用中是不正确的)
ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")
For the colours, can use scale_fill_brewer() (great tutorials here: http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/ ) 对于颜色,可以使用scale_fill_brewer()(这里很棒的教程: http : //www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/ )
ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")+scale_fill_brewer(palette="Blues")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.