R 一页上有多个条形图?
我试着用这个答案做与中相同的事情: 我的数据如下所示:R 一页上有多个条形图?,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我试着用这个答案做与中相同的事情: 我的数据如下所示: q r t s p a 757279 1414469 579865 527819 5951 b fa 4311006 5505875 10695932 5658172 63406 c wad 6479734 8194529 1490696 3154758 48312 我想要x轴上的行和y轴上的列。我试着用 df_t <-
q r t s p
a 757279 1414469 579865 527819 5951
b fa 4311006 5505875 10695932 5658172 63406
c wad 6479734 8194529 1490696 3154758 48312
我想要x轴上的行和y轴上的列。我试着用
df_t <- t(df)
df_t$id <- 1:nrow(df_t)
# Warning message:
#In distributions_t$id <- 1:nrow(distributions_t) : Coercing LHS to a list
dat <- melt(df_t,id.vars = "id")
ggplot(dat,aes(x=factor(id), y = value)) +
facet_wrap(~variable) +
geom_bar(aes(fill = factor(id)))
我怎样才能从这个答案中得到结果?我想你可以做如下(复制性的完整脚本)
库(重塑)
图书馆(GG2)
df这很好,但是当我有多行时,只有最下面的一行显示x轴标签,你知道如何解决这个问题吗?@NiekdeKlein使用facet_wrap(~variable,scales='free')
(老实说,你应该查看ggplot2文档;a引导我)
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
library(reshape)
library(ggplot2)
df <- data.frame(q=c(757279, 4311006, 6479734),
r=c(1414469, 5505875, 8194529),
t=c(579865, 10695932, 1490696),
s=c(527819, 5658172, 3154758),
p=c(5951, 63406, 48312),
row.names=c('a', 'b fa', 'c wad'))
df_t <- as.data.frame(t(df))
df_t$id <- 1:nrow(df_t)
dat <- melt(df_t,id.vars = "id")
p <- ggplot(dat, aes(x=factor(id), y=value)) +
geom_bar(stat='identity') +
facet_wrap(~variable)
p