简体   繁体   English

R:带有ggplot2包或barplot函数以给定角度书写的图例和标签的框

[英]R: A box for the legends and labels written at a given angle with ggplot2 package or barplot function

My question is certainly a repeat but I can't figure out how to achieve what I aim to make. 我的问题肯定是重复的,但我无法弄清楚如何实现我的目标。

Here is my data: 这是我的数据:

v1=c(46.55172, 13.79310, 29.31034,  1.72414,  5.17241,  3.44828,  0.00000,  0.60241, 24.09639, 59.63855,  4.81928,  6.02410,  0.00000,  4.81928, 14.58333, 22.91667, 58.33333, 0.00000,  2.08333,  2.08333,  0.00000, 20.96774, 20.96774, 47.58065,  5.64516,  3.22581,  0.80645,  0.80645)

names(v1) = c('Simul','SE','Obs','CG','Double','LR','RM','Simul','SE','Obs','CG','Double','LR','RM','Simul','SE','Obs', 'CG','Double','LR','RM','Simul','SE','Obs','CG', 'Double','LR','RM')

The first 7 numbers correspond to a fist "journal", the numbers from 8 to 14 corresponds to a second "journal", etc... 前7个数字对应第一个“新闻”,8到14个数字对应第二个“新闻”,依此类推。

The seven numbers of each journal have the names Simul, SE, Obs, CG, Double, LR, RM. 每个日志的七个数字分别命名为Simul,SE,Obs,CG,Double,LR,RM。 I want these numbers to represent the height of seven bars respectiviely in barplot and I want the 4 journals to be on the same window. 我希望这些数字分别代表小节中七个小节的高度,并且我希望这四个日志位于同一窗口中。 My current script does so. 我当前的脚本是这样做的。

par(mfrow=c(2,2))
for (journal in 0:3){
    if (journal == 0) { journal.name = 'American Naturalist'}
    if (journal == 1) { journal.name = 'Animal Behavour'}
    if (journal == 2) { journal.name = 'Ecology Letters'}
    if (journal == 3) { journal.name = 'Evolution'} 
    barplot(v1[((journal*7)+1):((journal*7)+7)],ylim=c(0,60),main=journal.name)
}
mtext('Frequency',padj=2,side=2,outer=T)
mtext('Articles Type',padj=-2,side=1,outer=T)

在此处输入图片说明

I now want to... 我现在要...

1) legend 1) 传说

... add a box (and the space for this box) on the right side in order to add some legend with the meaning of the abreviations (Simul, SE, OBS, etc...) ...在右侧添加一个框(以及该框的空格),以便添加一些带有缩写含义的图例(Simul,SE,OBS等)。

2) text angle 2) 文字角度

... write the abreviations (Simul, SE, OBS, etc...) with an angle of 45°. ...以45°的角度写出缩写(Simul,SE,OBS等)。

I guess the best way to achieve these things is to use ggplot but any answer types are welcome ! 我想实现这些目标的最好方法是使用ggplot,但是欢迎任何答案类型!

Thanks a lot ! 非常感谢 !

For starters, I would recommend reshaping your current data ( v1 ) to fit ggplot2 对于初学者,我建议重塑您的当前数据( v1 )以适合ggplot2

df = do.call("rbind",lapply(unique(names(v1)),function(x){v1[names(v1)==x]}))
rownames(df) = unique(names(v1))
colnames(df) = c("American Naturalist","Animal Behavour","Ecology Letters","Evolution")

head(df)
       American Naturalist Animal Behavour Ecology Letters Evolution
Simul             46.55172         0.60241        14.58333  20.96774
SE                13.79310        24.09639        22.91667  20.96774
Obs               29.31034        59.63855        58.33333  47.58065
CG                 1.72414         4.81928         0.00000   5.64516
Double             5.17241         6.02410         2.08333   3.22581
LR                 3.44828         0.00000         2.08333   0.80645

Now, using reshape2 : 现在,使用reshape2

head(melt(df))
    Var1                Var2    value
1  Simul American Naturalist 46.55172
2     SE American Naturalist 13.79310
3    Obs American Naturalist 29.31034
4     CG American Naturalist  1.72414
5 Double American Naturalist  5.17241
6     LR American Naturalist  3.44828

Next, a basic ggplot2 bar plot: 接下来,一个基本的ggplot2条形图:

p = ggplot(melt(df)) + geom_bar(aes(x=Var1,y=value, fill=Var1), stat="identity") + facet_wrap(~Var2)

The angle of axis labels: 轴标签角度:

p <- p + theme(axis.text.x = element_text(angle = 45))

在此处输入图片说明

I guess you can build on this by looking at labs for adding explanations for the axis labels. 我想您可以通过在labs中添加有关轴标签的解释来建立此基础。

As @Aaron said, it might be better to flip the plot around: 正如@Aaron所说,最好将情节转过来:

p + coord_flip()

在此处输入图片说明

OK, for one thing, let's put your data in a matrix. 好的,一方面,让我们将数据放在一个矩阵中。 Too hard to keep track in just a vector! 太难追踪了!

v2 <- matrix(v1, nrow=7)
rownames(v2) <-  c('Simul','SE','Obs','CG','Double','LR','RM')
colnames(v2) <- c('American Naturalist','Animal Behavour','Ecology Letters','Evolution')
v2
#        American Naturalist Animal Behavour Ecology Letters Evolution
# Simul             46.55172         0.60241        14.58333  20.96774
# SE                13.79310        24.09639        22.91667  20.96774
# Obs               29.31034        59.63855        58.33333  47.58065
# CG                 1.72414         4.81928         0.00000   5.64516
# Double             5.17241         6.02410         2.08333   3.22581
# LR                 3.44828         0.00000         2.08333   0.80645
# RM                 0.00000         4.81928         0.00000   0.80645

You're probably right that ggplot or lattice are going to be preferred solutions; 您可能是对的, ggplotlattice将成为首选解决方案; here's a lattice one. 这是一个lattice

library(lattice)
library(reshape2)
v3 <- melt(v2)
names(v3) <- c("Variable", "Journal", "Frequency")
barchart(Variable~Frequency|Journal, data=v3, as.table=TRUE)

在此处输入图片说明

Note that I've made the bars horizontal and that this way the labels for each bar can be easily read. 请注意,我已经将条形图水平放置,这样可以轻松地阅读每个条形图的标签。 This is preferred to putting them at an angle and giving your audience a pain in the neck. 最好是将它们倾斜一个角度并给听众带来颈部疼痛。 This also makes it possible to just use the full name of whatever those things are instead of just the abbreviations, rather than putting it in a legend and giving your audience whiplash. 这也使得仅使用这些名称的全名而不是缩写就成为可能,而不是将其放在图例中并给您的观众带来鞭打感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM