我有一个数据帧(df
),其结构如下:
df <- structure(list(header1 = structure(c(2L, 3L, 1L), .Label = c("bad",
"good", "so-so"), class = "factor"), header2 = structure(c(1L,
2L, 2L), .Label = c("bad", "good"), class = "factor"), header3 = structure(c(2L,
1L, 3L), .Label = c("bad", "good", "so-so"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
我一直在尝试使用plot_stackfrq
包中的sjPlot
来生成比例条形图。请注意,header2
列缺少任何“一般”值(仅header1和header3具有so-so
值)。我已经尝试过了,但是所得比例栏中的颜色显示不正确。与so-so
和header1
中的header3
对应的颜色在“ header2”列中显示为与good
对应的颜色。
是否有人使用sjPlot
或任何其他可以为每列创建比例条形图的R库解决方案?
答案 0 :(得分:3)
我不确定它给您带来什么问题。我设法产生了下面的图,它代表了样本df
中的数据。
library(sjPlot)
# get the index of columns to plot
start <- which(colnames(df)=="header1")
end <- which(colnames(df)=="header3")
plot_stackfrq(df[, start:end])
#If you want to set colors manually
# plot_stackfrq(df[, start:end], geom.colors = c("#56B4E9", "#009E73", "#0072B2"))
# Assign colors in alphabetical order bad -> good -> so-so.
df <- structure(list(header1 = structure(c(2L, 3L, 1L), .Label = c("bad",
"good", "so-so"), class = "factor"), header2 = structure(c(1L,
2L, 2L), .Label = c("bad", "good"), class = "factor"), header3 = structure(c(2L,
1L, 3L), .Label = c("bad", "good", "so-so"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))