我使用以下代码创建了一个数据框:
> p <- rep(c("5e-8", "0.05", "1"), 2)
> pgc1_rsq <- c(0.0037, 0.0726, 0.0847)
> meta_rsq <- c(0.0263, 0.1829, 0.1753)
> values <- c(pgc1_rsq, meta_rsq)
> Type <- c(rep("PGC1", 3), rep("PGC meta-analysis", 3))
> mydata <- data.frame(p, values)
> mydata$p <- factor(mydata$p, levels = c("5e-8", "0.05", "1"))
我使用以下代码创建了一个条形图:
> plot <-ggplot(mydata, aes(p, values))
> plot +geom_bar(stat = "identity", aes(fill = Type), position = "dodge") + xlab("P-value threshold") + ylab("Proportion of variance explained (Nagelkerke Rsq)")
我现在想重新订购这些酒吧 - 以便&#34; PGC1&#34;来自&#34; PGC荟萃分析&#34;在每一对。我试过制作两个&#34;值&#34;和&#34;键入&#34; PGC1的因素和排序水平首先出现如下:
> mydata$value <- factor(mydata$value, levels = c("pgc1_rsq", "pgc_meta"))
但这给了我一个错误信息&#34; Type&#34;并没有用&#34;值&#34;
产生预期的结果非常感谢输入和建议。感谢。
答案 0 :(得分:1)
为什么不在Type
中加入dataframe
:
p <- rep(c("5e-8", "0.05", "1"), 2)
pgc1_rsq <- c(0.0037, 0.0726, 0.0847)
meta_rsq <- c(0.0263, 0.1829, 0.1753)
values <- c(pgc1_rsq, meta_rsq)
Type <- c(rep("PGC1", 3), rep("PGC meta-analysis", 3))
mydata <- data.frame(p, values,Type)
mydata$Type <- factor(mydata$Type, levels = c("PGC1","PGC meta-analysis"))
mydata$p <- factor(mydata$p, levels = c("5e-8", "0.05", "1"))
plot <-ggplot(mydata, aes(p, values))
plot +geom_bar(stat = "identity", aes(fill = Type), position = "dodge") + xlab("P-value threshold") + ylab("Proportion of variance explained (Nagelkerke Rsq)")
答案 1 :(得分:0)
在geom_bar中尝试分解填充:
plot <- ggplot(mydata, aes(p, values))
plot + geom_bar(stat = "identity", aes(fill = factor(Type, levels = c("PGC1", "PGC meta-analysis"))), position = "dodge") +
xlab("P-value threshold") +
ylab("Proportion of variance explained (Nagelkerke Rsq)")
答案 2 :(得分:0)
稍微清理了数据框
mydata <- data.frame(p = factor(rep(c("5e-8", "0.05", "1"), 2), levels = c("5e-8", "0.05", "1")),
values =c(0.0037, 0.0726, 0.0847, 0.0263, 0.1829, 0.1753),
Type = factor(c( rep("PGC meta-analysis", 3),rep("PGC1", 3)), levels = c("PGC1", "PGC meta-analysis")))
ggplot(mydata, aes(p, values))+
geom_bar(stat = "identity", aes(fill = Type), position = "dodge") +
xlab("P-value threshold") +
ylab("Proportion of variance explained (Nagelkerke Rsq)")
答案 3 :(得分:0)
这些都是很好的答案。最优雅的方法是使用gather
中非常有用的tidyr
。大多数ggplot
s必须
library(dplyr)
library(tidyr)
library(ggplot2)
data.frame(p = c("5e-8", "0.05", "1"),
`pgc1_rsq` = c(0.0037, 0.0726, 0.0847), # change names here
`pgc_meta` = c(0.0263, 0.1829, 0.1753)) %>%
gather(Type, Value, 2:3) %>%
mutate(Type = factor(Type, levels = c("pgc1_rsq", "pgc_meta"))) %>% # and here if you need
ggplot(aes(x = p, y = Value, fill = Type)) +
geom_bar(stat = "identity", position = "dodge") +
xlab("P-value threshold") +
ylab("Proportion of variance explained (Nagelkerke Rsq)")