我有以下数据,尝试将其绘制为条形图和折线图(带CI)
或的特征,计数,奇数比和置信区间值的数据帧
我正在尝试获得一个情节,重叠的条形图和CI比率的折线图]
<<我尝试使用以下代码在ggplot2中进行绘图:
ggplot(feat)+
geom_bar(aes(x=Feat, y=Count),stat="identity", fill = "steelblue") +
geom_line(aes(x=Feat, y=OR*max(feat$Count)),stat="identity", group = 1) +
geom_point(aes(x=Feat, y=OR*max(feat$Count))) +
geom_errorbar(aes(x=Feat, ymin=CI1, ymax=CI2), width=.1, colour="orange",
position = position_dodge(0.05))
但是,我没有获得折线图的CI条,如图所示:相反,我得到的是条形图有人可以帮我解决这个问题。
谢谢
编辑-投放:
df <- structure(list(Feat = structure(1:8, .Label = c("A", "B", "C",
"D", "E", "F", "G", "H"), class = "factor"), Count = structure(c(2L,
8L, 7L, 5L, 4L, 1L, 6L, 3L), .Label = c("13", "145", "2", "25",
"26", "3", "37", "43"), class = "factor"), OR = structure(c(4L,
2L, 1L, 5L, 3L, 7L, 6L, 8L), .Label = c("0.38", "1.24", "1.33",
"1.51", "1.91", "2.08", "2.27", "3.58"), class = "factor"), CI1 = structure(c(7L,
4L, 1L, 6L, 3L, 5L, 2L, 2L), .Label = c("0.26", "0.43", "0.85",
"0.89", "1.2", "1.24", "1.25"), class = "factor"), CI2 = structure(c(3L,
2L, 1L, 6L, 4L, 7L, 8L, 5L), .Label = c("0.53", "1.7", "1.82",
"1.98", "13.07", "2.83", "3.92", "6.13"), class = "factor")), class = "data.frame", row.names = c(NA,
-8L))
我有以下数据,我试图将其绘制为条形图和折线图(带有CI),或者是特征,计数,奇数比和置信区间值的数据框,或者我正在尝试绘制图...] >
ratio <- max(feat$Count)/max(feat$CI2)
ggplot(feat) +
geom_bar(aes(x=Feat, y=Count),stat="identity", fill = "steelblue") +
geom_line(aes(x=Feat, y=OR*ratio),stat="identity", group = 1) +
geom_point(aes(x=Feat, y=OR*ratio)) +
geom_errorbar(aes(x=Feat, ymin=CI1*ratio, ymax=CI2*ratio), width=.1, colour="orange",
position = position_dodge(0.05)) +
scale_y_continuous("Count", sec.axis = sec_axis(~ . / ratio, name = "Odds Ratio"))
编辑:
只是为了使图例有趣。ggplot(feat) +
geom_bar(aes(x=Feat, y=Count, fill = "Count"),stat="identity") + scale_fill_manual(values="steelblue") +
geom_line(aes(x=Feat, y=OR*ratio, color = "Odds Ratio"),stat="identity", group = 1) + scale_color_manual(values="orange") +
geom_point(aes(x=Feat, y=OR*ratio)) +
geom_errorbar(aes(x=Feat, ymin=CI1*ratio, ymax=CI2*ratio), width=.1, colour="orange",
position = position_dodge(0.05)) +
scale_y_continuous("Count", sec.axis = sec_axis(~ . / ratio, name = "Odds Ratio")) +
theme(legend.key=element_blank(), legend.title=element_blank(), legend.box="horizontal",legend.position = "bottom")
df
。# convert factor to numeric column
df$Count <- as.numeric(as.character(df$Count))
df$OR <- as.numeric(as.character(df$OR))
df$CI1 <- as.numeric(as.character(df$CI1))
df$CI2 <- as.numeric(as.character(df$CI2))
ggplot(data = df)+
geom_bar(aes(x=Feat, y=Count), stat="identity", fill = "steelblue", position = position_dodge(width = 0.05)) +
geom_errorbar(aes(x=Feat, ymin=Count+CI1, ymax=Count+CI2), width=.1, colour="black") +
geom_line(aes(x=Feat, y=OR*max(Count)), stat="identity", group = 1) +
geom_point(aes(x=Feat, y=OR*max(Count)))+
geom_errorbar(aes(x=Feat, ymin=OR*max(Count)+CI1, ymax=OR*max(Count)+CI2), width=.1, colour="black")
df
。# convert factor to numeric column
df$Count <- as.numeric(as.character(df$Count))
df$OR <- as.numeric(as.character(df$OR))
df$CI1 <- as.numeric(as.character(df$CI1))
df$CI2 <- as.numeric(as.character(df$CI2))
ggplot(data = df)+
geom_bar(aes(x=Feat, y=Count), stat="identity", fill = "steelblue", position = position_dodge(width = 0.05)) +
geom_errorbar(aes(x=Feat, ymin=Count+CI1, ymax=Count+CI2), width=.1, colour="black") +
geom_line(aes(x=Feat, y=OR*max(Count)), stat="identity", group = 1) +
geom_point(aes(x=Feat, y=OR*max(Count)))+
geom_errorbar(aes(x=Feat, ymin=OR*max(Count)+CI1, ymax=OR*max(Count)+CI2), width=.1, colour="black")