如何使我的数据点与点并排 [英] how can I make my data side by side barplot with dots
问题描述
我已阅读此帖子
我尝试了很多事情,但我做不到.
我得到的最接近的是以下代码
par(mfrow = c(1,2))barplot(dfm $ Freq.y,axes = F,horiz = T,axisnames = FALSE)轴(侧面= 1,在=序列(1,252,50))barplot(dfm $ Freq.x,horiz = T,axes = T,las = 1)
如果您只是将图形绘制为上述文章,您会发现它为我的数据提供了错误的数字例如
库(网格)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))+主题(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(销售人员数量")+主题(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)")+主题(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()图书馆(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))
见情节并查看例如两个df的F2值你看?两者都是最高值,但在此图中它们显示的值最少关于第二个答案.他们融化了数据,这让我疯狂地融化了我的数据所以我无法弄清楚如何根据该图进行绘制大约是第三个,这完全不符合我的兴趣,因为我只需要一个数据图就可以了,而不必分割
分配g.mid之后,它没有考虑顺序,它从87开始到结尾,而我的 dfm
的顺序从2开始
用于红色和蓝色点.您可以添加 + geom_point(aes(x = Var1,y = Freq.x),shape = 19,size = 6,color ="red")+
和 geom_point(aes(x= Var1,y = Freq.y),形状= 19,尺寸= 6,颜色="blue")+
.尝试以下代码(在@ cuttlefish44的帮助下):
库(dplyr)ind<-gsub("F",",dfm $ Var1)%>%as.numeric()%&%;%顺序(递减= T)newlev<-level(dfm $ Var1)[ind]dfm $ Var1<-因数(dfm $ Var1,级别= newlev)图书馆(网格)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))+主题(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(销售人员数量")+geom_point(aes(x = Var1,y = Freq.x),shape = 19,size = 6,color ="red")+主题(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")+主题(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()图书馆(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
在此示例中,我修改了标签.尝试以下代码:
库(ggplot2)图书馆(dplyr)ind<-gsub("F",",dfm $ Var1)%>%as.numeric()%&%;%顺序(递减= T)newlev<-level(dfm $ Var1)[ind]dfm $ Var1<-因数(dfm $ Var1,级别= newlev)#将35级标记为7级图书馆(plyr)每个5级(dfm $ Var1)interval< -rep(c(rep(c(FALSE),each = 4),TRUE),length.out = 35)dfm $ Var2< -c(")为(i in 1:length(dfm $ Var1)){如果(dfm $ Var1 [i]%in%each5 [interval])dfm $ Var2 [i]-as.character(dfm $ Var1 [i])}图书馆(网格)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" =剂量0.5","F10" =剂量1","F21" =剂量2"))+主题(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(销售人员数量")+geom_point(aes(x = Var1,y = Freq.x),shape = 19,size = 6,color ="red")+expand_limits(y = 600)+主题(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)+主题(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()图书馆(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))
I have read this post Two horizontal bar charts with shared axis in ggplot2 (similar to population pyramid) and similar ones which could not help me to solve my problem because my data is different. Where I try to explain below
I have a data like below
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")
I have a mutual column named Var1 and two columns Freq.x and Freq.y
I want to plot barplot in vertical ways and put a red
or blue
dot in top of each bar.
I want to plot the Freq.x in the right side, Freq.y in the left side and the F2 to ... in the middle of both as Y axis
Something like this
I have tried many things but I could not make it.
The closest I got is with the following code
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)
If you just plot the figure as the post mentioned above, you see that it is giving wrong figure for my data for example
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))
See the plot and look at for example the F2 values for both dfs you see? both are the highest values but in this plot they show the least about the second answer. they guy Melted the data and it made me crazy to melt my data so I could not figure it out how to plot it based on that about the third, it is completely out of my interest because I only need one plot with my data and not splitting it
after assigning the g.mid , it does not take the order into account , it starts from 87 to the end while the order of my dfm
starts from 2
For the red and blue dots. You can add +geom_point(aes(x = Var1, y = Freq.x),shape=19,size=6,color="red")+
and geom_point(aes(x = Var1, y = Freq.y),shape=19,size=6,color="blue")+
. Try this code (with @cuttlefish44 's help):
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))
UPDATE Nº4
In this example, I modify the labels. Try this code:
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))
这篇关于如何使我的数据点与点并排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!