我已经阅读了这篇文章Two horizontal bar charts with shared axis in ggplot2 (similar to population pyramid)以及类似的帖子,这些文章无法解决我的问题,因为我的数据不同。我试着在下面解释
我有以下数据
dfm<- structure(list(Var1 = structure(1:35, .Label = c("F10", "F11",
"F12", "F13", "F14", "F15", "F16", "F18", "F19", "F2", "F21",
"F25", "F3", "F30", "F35", "F4", "F5", "F58", "F6", "F60", "F7",
"F79", "F8", "F9", "F97", "F117", "F17", "F22", "F23", "F26",
"F31", "F41", "F61", "F67", "F87"), class = "factor"), Freq.x = c(4L,
1L, 2L, 3L, 4L, 1L, 2L, 2L, 1L, 252L, 1L, 2L, 106L, 1L, 1L, 56L,
32L, 1L, 28L, 1L, 17L, 1L, 10L, 7L, 1L, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), Freq.y = c(12L, 3L, 6L, NA, 7L, NA, 1L, NA,
2L, 306L, 1L, 2L, 170L, NA, NA, 69L, 45L, NA, 35L, NA, 20L, NA,
13L, 7L, NA, 1L, 3L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L)), .Names = c("Var1",
"Freq.x", "Freq.y"), row.names = c(NA, -35L), class = "data.frame")
我有一个名为Var1的共有列和两列Freq.x和Freq.y
我想以垂直方式绘制条形图,并在每个条形图的顶部放置red
或blue
点。
我想在右侧绘制Freq.x,在左侧绘制Freq.y,在两个中间绘制F2到...作为Y轴
像这样的东西
我尝试了很多东西,但我无法做到。
我最接近的是以下代码
par(mfrow=c(1,2))
barplot(dfm$Freq.y,axes=F,horiz=T,axisnames=FALSE)
axis(side=1,at=seq(1,252,50))
barplot(dfm$Freq.x,horiz=T,axes=T,las=1)
如果您只是将图形绘制为上述帖子, 你看到它为我的数据提供了错误的数字 例如
library(grid)
g.mid<-ggplot(dfm,aes(x=1,y=Var1))+geom_text(aes(label=Var1))+
geom_segment(aes(x=0.94,xend=0.96,yend=Var1))+
geom_segment(aes(x=1.04,xend=1.065,yend=Var1))+
ggtitle("")+
ylab(NULL)+
scale_x_continuous(expand=c(0,0),limits=c(0.94,1.065))+
theme(axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background=element_blank(),
axis.text.x=element_text(color=NA),
axis.ticks.x=element_line(color=NA),
plot.margin = unit(c(1,-1,1,-1), "mm"))
g1 <- ggplot(data = dfm, aes(x = Var1, y = Freq.x)) +
geom_bar(stat = "identity") + ggtitle("Number of sales staff") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(1,-1,1,0), "mm")) +
scale_y_reverse() + coord_flip()
g2 <- ggplot(data = dfm, aes(x = Var1, y = Freq.y)) +xlab(NULL)+
geom_bar(stat = "identity") + ggtitle("Sales (x $1000)") +
theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.text.y = element_blank(), axis.ticks.y = element_blank(),
plot.margin = unit(c(1,0,1,-1), "mm")) +
coord_flip()
library(gridExtra)
gg1 <- ggplot_gtable(ggplot_build(g1))
gg2 <- ggplot_gtable(ggplot_build(g2))
gg.mid <- ggplot_gtable(ggplot_build(g.mid))
grid.arrange(gg1,gg.mid,gg2,ncol=3,widths=c(4/9,1/9,4/9))
看情节 并查看两个dfs的F2值 你看?两者都是最高值,但在这个图中它们显示最少 关于第二个答案。他们把数据融化了,这让我疯狂地融化了我的数据 所以我无法弄清楚如何基于此绘制它 关于第三个,这完全出于我的兴趣,因为我只需要一个包含我的数据的图而不是将它拆分
分配g.mid后,它不会考虑顺序,它从87开始到结束,而我的dfm
的顺序从2开始
答案 0 :(得分:3)
红色和蓝色圆点。您可以添加+geom_point(aes(x = Var1, y = Freq.x),shape=19,size=6,color="red")+
和geom_point(aes(x = Var1, y = Freq.y),shape=19,size=6,color="blue")+
。试试这段代码(使用@ cuttlefish44的帮助):
library(dplyr)
ind <- gsub("F", "", dfm$Var1) %>% as.numeric() %>% order(decreasing = T)
newlev <- levels(dfm$Var1)[ind]
dfm$Var1 <- factor(dfm$Var1, levels = newlev)
library(grid)
g.mid<-ggplot(dfm,aes(x=1,y=Var1))+geom_text(aes(label=Var1))+
geom_segment(aes(x=0.94,xend=0.96,yend=Var1))+
geom_segment(aes(x=1.04,xend=1.065,yend=Var1))+
ggtitle("")+
ylab(NULL)+
scale_x_continuous(expand=c(0,0),limits=c(0.94,1.065))+
theme(axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background=element_blank(),
axis.text.x=element_text(color=NA),
axis.ticks.x=element_line(color=NA),
plot.margin = unit(c(1,-1,1,-1), "mm"))
g1 <- ggplot(data = dfm, aes(x = Var1, y = Freq.x)) +
geom_bar(stat = "identity") + ggtitle("Number of sales staff") +
geom_point(aes(x = Var1, y = Freq.x),shape=19,size=6,color="red")+
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(1,-1,1,0), "mm")) +
scale_y_reverse() + coord_flip()
g2 <- ggplot(data = dfm, aes(x = Var1, y = Freq.y)) +xlab(NULL)+
geom_bar(stat = "identity") + ggtitle("Sales (x $1000)") +
geom_point(aes(x = Var1, y = Freq.y),shape=19,size=6,color="blue")+
theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.text.y = element_blank(), axis.ticks.y = element_blank(),
plot.margin = unit(c(1,0,1,-1), "mm")) +
coord_flip()
library(gridExtra)
gg1 <- ggplot_gtable(ggplot_build(g1))
gg2 <- ggplot_gtable(ggplot_build(g2))
gg.mid <- ggplot_gtable(ggplot_build(g.mid))
grid.arrange(gg1,gg.mid,gg2,ncol=3,widths=c(4/9,1/9,4/9))
更新Nº4
在此示例中,我修改了标签。试试这段代码:
library(ggplot2)
library(dplyr)
ind <- gsub("F", "", dfm$Var1) %>% as.numeric() %>% order(decreasing = T)
newlev <- levels(dfm$Var1)[ind]
dfm$Var1 <- factor(dfm$Var1, levels = newlev)
#labeling 35 levels to 7 levels
library(plyr)
each5<-levels(dfm$Var1)
interval<-rep(c(rep(c(FALSE),each=4),TRUE),length.out= 35)
dfm$Var2<-c("")
for (i in 1:length(dfm$Var1)){
if (dfm$Var1[i] %in% each5[interval])
dfm$Var2[i]<-as.character(dfm$Var1[i])
}
library(grid)
g.mid<-ggplot(dfm,aes(x=1,y=Var1))+
geom_text(aes(label= Var2),fontface=2)+
geom_segment(aes(x=0.94,xend=0.96,yend=Var1))+
geom_segment(aes(x=1.04,xend=1.065,yend=Var1))+
ggtitle("")+
ylab(NULL)+
scale_x_continuous(expand=c(0,0),limits=c(0.94,1.065))+
scale_y_discrete(labels=c("F2" = "Dose 0.5", "F10" = "Dose 1",
"F21" = "Dose 2"))+
theme(axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background=element_blank(),
axis.text.x=element_text(color=NA),
axis.ticks.x=element_line(color=NA),
plot.margin = unit(c(1,-1,1,-1), "mm"))
g1 <- ggplot(data = dfm, aes(x = Var1, y = Freq.x)) +
geom_bar(stat = "identity") + ggtitle("Number of sales staff") +
geom_point(aes(x = Var1, y = Freq.x),shape=19,size=6,color="red")+
expand_limits(y =600) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(1,-1,1,0), "mm")) +
scale_y_reverse() + coord_flip()
g2 <- ggplot(data = dfm, aes(x = Var1, y = Freq.y)) +xlab(NULL)+
geom_bar(stat = "identity") + ggtitle("Sales (x $1000)") +
geom_point(aes(x = Var1, y = Freq.y),shape=19,size=6,color="blue")+
expand_limits(y =600) +
theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.text.y = element_blank(), axis.ticks.y = element_blank(),
plot.margin = unit(c(1,0,1,-1), "mm")) +
coord_flip()
library(gridExtra)
gg1 <- ggplot_gtable(ggplot_build(g1))
gg2 <- ggplot_gtable(ggplot_build(g2))
gg.mid <- ggplot_gtable(ggplot_build(g.mid))
grid.arrange(gg1,gg.mid,gg2,ncol=3,widths=c(4/9,1/9,4/9))
答案 1 :(得分:2)