R ggplot barplot;基于两个单独的变量填充 [英] R ggplot barplot; Fill based on two separate variables

查看:141
本文介绍了R ggplot barplot;基于两个单独的变量填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一张图片说了超过一千个字。正如你所看到的,我的 fill 是基于变量 variable 。因为离散变量复杂性使它们唯一,所以在每个条中都有多个数据实体(黑色边框)。我试图找到的东西使得每个栏的区域比当前的外观更加可区分。如果是像阴影那样的话,最好是。



以下是一个示例(自导入原始数据集以来不同):

  dat < -  read.table(text =Complexity Method Sens Spec MMC 
1 L Alpha 50 20 10
2 M Alpha 40 30 80
3 H Alpha 10 10 5
4 L Beta 70 50 60
5 M Beta 49 10 80
6 H Beta 90 17 48
7 L伽玛19 5 93
8 M伽马18 39 4
9 H伽玛10 84 74,sep =,header = T)

library(ggplot2)
library(reshape)
short.m< - melt(dat)
ggplot(short.m,aes(x = Method,y = value / 100,fill = variable))+
geom_bar(stat =identity,position =dodge,color =black)+
coord_flip()


解决方案

这远非完美,但希望朝着正确的方向迈出一步,因为它被 vari能够,但仍以某种方式代表 Complexity

  ggplot(short.m,aes(x = Method,y = value / 100,group = variable,fill = variable,alpha = Complexity,))+ 
geom_bar(stat =identity ,位置=闪避,颜色=黑色)+
scale_alpha_manual(值= c(0.1,0.5,1))+
coord_flip()


A picture says more than a thousand words. As you can see, my fill is based on the variable variable.

Within each bar there is however multiple data entities (black borders) since the discrete variable complexity make them unique. What I am trying to find is something that makes each section of the bar more distinguishable than the current look. Preferable would be if it was something like shading.

Here's an example (not the same dataset, since the original was imported):

dat <- read.table(text = "Complexity Method Sens Spec MMC
1 L Alpha 50 20 10
2 M Alpha 40 30 80
3 H Alpha 10 10 5
4 L Beta 70 50 60
5 M Beta 49 10 80
6 H Beta 90 17 48
7 L Gamma 19 5 93
8 M Gamma 18 39 4
9 H Gamma 10 84 74", sep = "", header=T)

library(ggplot2)
library(reshape)
short.m <- melt(dat)
ggplot(short.m, aes(x=Method, y= value/100 , fill=variable)) + 
    geom_bar(stat="identity",position="dodge", colour="black") + 
    coord_flip()

解决方案

This is far from perfect, but hopefully a step in the right direction, as it's dodged by variable, but still manages to represent Complexity in some way:

ggplot(short.m, aes(x=Method, y=value/100, group=variable, fill=variable, alpha=Complexity,)) + 
  geom_bar(stat="identity",position="dodge", colour="black") +
  scale_alpha_manual(values=c(0.1, 0.5, 1)) +
  coord_flip()

这篇关于R ggplot barplot;基于两个单独的变量填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆