与3个变量barplot(连X和Y和第三堆叠变量)(barplot with 3 variables

2019-08-06 01:25发布

我有这样一些数据:

myd <- structure(list(var1 = structure(1:4, .Label = c("II", "III", 
       "IV", "V"), class = "factor"), zero_co = c(15.15152, 3.030303, 
        0, 0), non_zero_CO = c(84.84848, 96.969697, 100, 100), size = c(230, 
        813, 317, 1532)), .Names = c("var1", "zero_co", "non_zero_CO", 
        "size"), row.names = c(NA, -4L), class = "data.frame")

# myd
# I                   II         III   IV     V  
# zero_co       15.15152    3.030303    0     0
# non-zero CO   84.84848   96.969697  100   100
# size         230.00000  813.000000  317  1532

我要绘制size在y轴和其它两个变量zero_conon-zero CO作为x轴的层叠条。 我想利用这个绘制gplotsggplots但找到的困难。 如何绘制呢?

Answer 1:

这里是如果我的理解是正确的解决方案。 当你有机会利用X定量变量,y轴你不能做柱状图。 您需要使用矩形(看起来像吧反正)。

myd <- data.frame (var1 = c("II", "III", "IV", "V"), zero_co = c(15.15152 , 3.030303,    0,     0),
             non_zero_CO = c(84.84848,   96.969697,  100,   100),
              size = c(230.00000,  813.000000,  317,  1532))

    require(ggplot2)

ggplot(myd) + geom_rect(aes(xmin = 0, xmax = zero_co, ymin =size , ymax =size + 80 ), fill = "lightgreen") +
geom_rect(aes(xmin = zero_co, xmax = zero_co + non_zero_CO, ymin =size , ymax =size + 80 ), fill = "darkblue") + theme_bw()

给你的情节:



Answer 2:

这里就是我能得到根据我有限的了解:

myd <- data.frame (var1 = c("II", "III", "IV", "V"), zero_co = c(15.15152 , 3.030303,    0,     0),
             non_zero_CO = c(84.84848,   96.969697,  100,   100),
              size = c(230.00000,  813.000000,  317,  1532))
myd1 <- as.matrix (t(myd[,2:3]))

barplot(myd1)



Answer 3:

我不知道你怎么想的最后情节看似但这里有一个ggplot2建议。

首先,重塑数据为长格式:

library(reshape2)
myd_long <- melt(myd, measure.vars = c("zero_co", "non_zero_CO"))

计算绝对值(我假设value中所占的百分比size 。):

myd_long <- within(myd_long, valueAbs <- size * value / 100)

情节:

library(ggplot2)    

ggplot(myd_long, aes(y = valueAbs, x = var1, fill = variable)) +
  geom_bar(stat = "identity")



文章来源: barplot with 3 variables (continous X and Y and third stacked variable)