问题描述
我有以下代码:
library(ggplot2)
K <- data.frame(KK=c("30", "30", "30", "30","10", "10", "10", "10"),k=c("10", "8", "5", "2","10", "8", "5", "2"),
Precision=c(85.2,87.5,100,100,82.5,83.3,85.2,94.4),
Recall=c(73.3,80,100,100,51.4,54.8,61.1,87.9) ,
Fscore=c(70.8,79.4,100,100,49.1,54.2,62.7,90.3),
Accuracy=c(82.2,86.7,100,100,63.3,66.7,73.3,93.3))
df2 <- reshape2::melt(K, 1:2)
ggplot(df2,
aes(k, value, fill = variable)) +
geom_bar(stat = 'identity', position = 'dodge') +
theme(legend.position = 'top')
这段代码给了我下面的情节.
This code gives me the following plot.
但是,我想获得一个这样的barplot
However, I want to get a barplot like this
k的每个值(10,8,5,2)应该是一组条形,每个条形的颜色都是一个度量.此外,KK值30的小节应为实线,并且KK为10.我不知道是否清楚.在我的输出中显示K30的值,但与K10合并并与剥离的K30丢失.
Each value of k (10,8,5,2) should be a group of bars and each colour of bar a metric. In addition, the bar from the KK value 30 should be solid and with KK of 10 stripped. I don't know if it is clear. In my output appears the values for K30, but missing with K10 merged with K30 stripped.
推荐答案
您可以为每个KK
值简单地将两个不同的图层添加到绘图中.不幸的是,ggplot
不能很好地处理模式(或根本无法很好地处理模式),请参见以下文章: colors-in-ggplot2/2901210#2901210>如何在ggplot2中添加纹理以填充颜色?
You could simply add the two different layers to your plot one for each KK
value. Unfortunately, ggplot
does not handle patterns well (or at all really), see this post: How to add texture to fill colors in ggplot2?
为每个KK
值添加不同图层的代码为:
The code to add different layers for each KK
value is:
ggplot() +
geom_bar(data=df2[which(df2$KK==10),], aes(k, value, fill = variable),stat = 'identity',position="dodge") +
geom_bar(data=df2[which(df2$KK==30),], aes(k, value, fill = variable),stat = 'identity',position="dodge",alpha=0.5) +
theme(legend.position = 'top')
这篇关于具有不同R填充的R堆叠分组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!