2015-09-30 27 views
2

我的数据帧:自定义线(多barplots)

N P E S 
W1 3.5 3.5 3.4 3.5 
w2 3.4 3.7 3.6 3.5 
w3 3.5 3.4 3.5 3.5 
w4 3.5 3.4 3.5 3.5 
w5 3.5 3.4 3.5 3.5 
w6 3.5 3.4 3.5 3.5 
w7 3.5 3.4 3.5 3.5 
w8 3.5 3.4 3.5 3.5 

和代码barplot

mat <- as.matrix(t(tabela.matrix)) 

qw <- barplot(mat, beside = TRUE, axisnames = FALSE, ylim = c(0, 6), 
       col = c("yellow", "deepskyblue1", "yellowgreen", "orchid4")) 

text(colMeans(qw[2:3, ]), -0.25, labels = colnames(mat), xpd = TRUE, srt = 15) 
text(qw, 0, round(as.matrix(t(tabela.matrix)), 1), cex = 1, pos = 3, srt = 90) 

legend(3.2, -0.6, c("N","P","E", "S"), 
     fill = c("yellow", "deepskyblue1", "yellowgreen", "orchid4"), 
     horiz = TRUE, xpd = TRUE) 

产生这种barplot enter image description here

所以我barplot我需要添加line表示减法:每行最大 - 最小值。所以我使用下面的代码来获得减法。

max.tmp <- apply(df, 1, max) 
min.tmp <- apply(df, 1, min) 
substr.tmp <- max.tmp - min.tmp 

现在我想在substr.tmp中添加红线指示值。代码lines()不起作用。

我的输出(红线)应该“看”这样的(这是图片格式的Excel和我有兴趣在图片本身添加行,而不是数据)

enter image description here


structure(c(3.46666666666667, 3.35, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.45555555555556, 3.7, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.40769230769231, 3.57692307692308, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.51122994652406, 3.5156862745098, 3.49090909090909, 3.49090909090909, 3.49090909090909, 3.49090909090909, 3.49090909090909, 3.49090909090909), .Dim = c(8L, 4L), .Dimnames = list(c("a", "b", "c", "d", "e", "f", "g", "h"), NULL)) 
+0

你可以添加数据帧'dput' – rbm

+0

@rbm,这里它是**结构(c(3.46666666666667,3.35,3.5 ,3.5,3.5,3.5,3.5, 3.5,3.45555555555556,3.7,3.36666666666667,3.36666666666667, 3.36666666666667,3.36666666666667,3.36666666666667,3.36666666666667, 3.40769230769231,3.57692307692308,3.53846153846154,3.53846153846154, 3.53846153846154,3.53846153846154,3.53846153846154,3.53846153846154, 3.51122994652406, 3.5156862745098,3.49090909090909,3.49090909090909, 3.49090909090909,3.49090909090909,3.49090909090909,3.49090909090909, ) ,.Dim = c(8L,4L),.Dimnames = list(c(“a”,“b”,“c”,“d”,“e”, “f”,“g”,“h” ),NULL))'** – Miha

+0

你的图形是不同的 - 首先在y轴上0到3.5,第二个接近6。同样在'max.tmp < - apply(df,1,max)'它不是清楚'df'是什么..你可以用'dput' init发布整个可运行的例子。 – rbm

回答

4

随着data.tableggplot2包,你可以做到这一点,如下所示:数据的dput

library(data.table) 
df2 <- melt(setDT(df), id="name") 
df2[, difference := max(value) - min(value), by = name] 

library(ggplot2) 
ggplot(df2, aes(x=name, y=value, fill=variable)) + 
    geom_bar(stat="identity", position="dodge") + 
    geom_line(aes(x=name, y=difference, group=1), size=1.5, color="red") + 
    scale_x_discrete(expand = c(0,0)) + 
    scale_y_continuous(expand = c(0,0), limits = c(0,4)) + 
    scale_fill_manual(breaks = c("V1", "V2", "V3", "V4"), 
        values = c("yellow", "deepskyblue1", "yellowgreen", "orchid4")) + 
    theme_bw() 

这给:

enter image description here

使用的数据

m <- structure(c(3.46666666666667, 3.35, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.45555555555556, 3.7, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.36666666666667, 3.40769230769231, 3.57692307692308, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.53846153846154, 3.51122994652406, 3.5156862745098, 3.49090909090909, 3.49090909090909, 3.49090909090909, 3.49090909090909, 3.49090909090909, 3.49090909090909), .Dim = c(8L, 4L), .Dimnames = list(c("a", "b", "c", "d", "e", "f", "g", "h"), NULL)) 
df <- as.data.frame(m) 
df$name <- rownames(df) 
+0

完美的是我所需要的。有用。 – Miha

2

这可能为你工作,但在你的例子数据集(的差异与一个在您的意见一起)会产生一个有趣的外观:

#original code 
mat <- as.matrix(t(tabela.matrix)) ;qw <- barplot(mat, beside=TRUE, axisnames = FALSE,ylim = c(0, 6), col=c("yellow", "deepskyblue1", "yellowgreen","orchid4")) ; text(colMeans(qw[2:3,]), -0.25, labels = colnames(mat), xpd=TRUE, srt=15); legend(3.2,-0.6, c("Nadrejeni","Podrejeni","Enak nivo", "Samoocenjevalec"), fill=c("yellow", "deepskyblue1", "yellowgreen","orchid4"), horiz=TRUE, xpd=TRUE) 
text(qw, 0, round(as.matrix(t(tabela.matrix)), 1),cex=1,pos=3,srt=90) 
legend(3.2, -0.6, c("N","P","E", "S"), 
    fill = c("yellow", "deepskyblue1", "yellowgreen", "orchid4"), 
    horiz = TRUE, xpd = TRUE) 
#new code 
par(new=TRUE) 
plot(substr.tmp, lwd=2, col="red", type="l", axes=FALSE, ylim=c(0,6)) 
axis(4, ylim=c(0,6), col="red",col.axis="red",las=1) 

请注意,此图有两个轴,一个黑色,一个红色。红色是第二个情节。如果你想删除它,请注释最后一行。并根据您的预期输出,这是保持在原来的规模(因此ylim=c(0,6)