本文介绍了如何使用R中的ggplot / geom_bar从数据集的顶部添加自定义标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有附加的数据集并使用此R代码来绘制数据: plotData< - read.csv( plotdata.csv) ix long scale_x_continuous(break = ix)+ labs(y ='吞吐量(Mbps)',x ='节点')+ scale_fill_discrete(name =Legend, labels = c(Inside Firewall Dest),Inside Firewall(Source),Outside Firewall(Dest),Outside Firewall(Source)))+ theme(legend。位置=right)+#图例主题的位置(legend.title = element_text(color =blue,size = 14,face =bold))+#标题外观主题(legend.text = element_text(color =blue,size = 12,face =bold))#标签外观 plot(ggp2) 现在我需要在不同的数据集中添加数字每个酒吧。例如:在内部防火墙(Dest)之上的 应该是来自sampleNumIFdest.csv的数字。 $ b $ Inside Firewall(Source)之上的b 应该是来自sampleNumIFsource.csv的数字。Outside Firewall(Dest)之上的 应该是来自sampleNumOFdest的数字。在Outside Firewall(Source)之上的csv 应该是来自sampleNumOFsource.csv的数字 我试图使用 geom_text(),但我不知道如何读取不同数据集中的数字。请注意,数据集具有不同的行数(这对我造成了额外的问题)。任何建议,高度赞赏。 对不起,我不得不压缩所有文件,因为我不允许在我的文章中添加2个以上的URL。 / p> 解决方案我认为最好的解决方案是将所有的数据集合成一个: #加载不同的数据集 plotData< - read.csv(plotData.csv) IFdest< - read.table(sampleNumIFdest。 csv,sep =\ t,header = TRUE,strip.white = TRUE) IFsource< - read.table(sampleNumIFsource.csv,sep =\t,header = TRUE ,strip.white = TRUE) OFdest< - read.table(sampleNumOFdest.csv,sep =\t,header = TRUE,strip.white = TRUE) OFsource< - read.table(sampleNumOFsource.csv,sep =\t,header = TRUE,strip.white = TRUE) #添加一个id ix plotData $ id< - 1:nrow(plotData)p lotData< - plotData [,c(5,1,2,3,4)] #组合不同的数据帧 plotData $ IFdest< -c(IFdest $ Freq,NA ) plotData $ IFsource< -c(IFsource $ Freq,NA,NA) plotData $ OFdest< - OFdest $ Freq plotData $ OFsource< -c(OFsource $ Freq, NA,NA) #重塑数据帧 long< - cbind( melt(plotData,id = c(id),measure = c(2:5 ), variable =type,value.name =value), melt(plotData,id = c(id),measure = c(6:9),变量=名称,value.name =数字)) long 完成后,您可以使用 geom_text 来绘制数字在酒吧之上: #创建您的图 ggplot(long,aes(x = id,y = value,fill = type))+ geom_bar(stat =identity,position =dodge)+ geom_text(aes(label = numbers),vjust = -1,position = position_dodge( 0。 9),size = 3)+ scale_x_continuous(breaks = ix)+ labs(x =Nodes,y =Throughput(Mbps))+ scale_fill_discrete(name =传说,标签= c(内部防火墙(Dest),内部防火墙(源),外部防火墙(Dest), (source)))+ theme_bw()+ theme(legend.position =right)+ theme(legend.title = element_text(color =blue,size = 14),face =bold))+ theme(legend.text = element_text(color =blue,size = 12,face =bold)) 结果: 正如您所看到的,文本标签有时会重叠。您可以通过减小文字的大小来改变这种情况,但是您会冒着标签变得难以阅读的风险。因此,您可能会考虑通过将 facet_grid(type〜。)(或 facet_wrap(〜type))添加到facet绘图代码: $ g $ p $ gcplot(long,aes(x = id,y = value,fill = type))+ geom_bar(stat =identity,position =dodge)+ geom_text(aes(label = numbers),vjust = -0.5,position = position_dodge(0.9),size = 3)+ scale_x_continuous(Nodes,breaks = ix)+ scale_y_continuous(Throughput(Mbps),limits = c(0,1000))+ scale_fill_discrete(name =Legend,标签= c(内部防火墙(Dest),内部防火墙(源),外部防火墙(Dest),防火墙外(源) + theme_bw()+ 主题(legend.position =right)+ 主题(legend.title = element_text(color =blue,size = 14,face =bold ))+ 主题(legend.text = element_text(color =blue,size = 12, face =bold))+ facet_grid(type〜。) 如下图: I have the attached datasets and use this R code to plot the data:plotData <- read.csv("plotdata.csv")ix <- 1:nrow(plotData)long <- melt(transform(plotData, id = ix), id = "id") # add id col; melt to long formggp2 <- ggplot(long, aes(id, value, fill = variable))+geom_bar(stat = "identity", position = "dodge")+ scale_x_continuous(breaks = ix) + labs(y='Throughput (Mbps)',x='Nodes') + scale_fill_discrete(name="Legend", labels=c("Inside Firewall (Dest)", "Inside Firewall (Source)", "Outside Firewall (Dest)", "Outside Firewall (Source)")) + theme(legend.position="right") + # The position of the legend theme(legend.title = element_text(colour="blue", size=14, face="bold")) + # Title appearance theme(legend.text = element_text(colour="blue", size = 12, face = "bold")) # Label appearanceplot(ggp2)The resulting plot is attached as well.Now I need to add numbers from different datasets on top of each bar. For example:on top of "Inside Firewall (Dest)" should be the numbers from sampleNumIFdest.csvon top of "Inside Firewall (Source)" should be the numbers from sampleNumIFsource.csvon top of "Outside Firewall (Dest)" should be the numbers from sampleNumOFdest.csvon top of "Outside Firewall (Source)" should be the numbers from sampleNumOFsource.csvI have tried to use geom_text() but I do not know how to read the numbers from the different datasets. Please note, that the datasets have different number of rows (which causes additional problems for me). Any suggestion is highly appreciated.The attached files are here.Sorry, I had to zip all my files as I am not allowed to add more then 2 URLs in my post. 解决方案 I think the best solution is to combine all the datasets into one:# loading the different datasetsplotData <- read.csv("plotData.csv")IFdest <- read.table("sampleNumIFdest.csv", sep="\t", header=TRUE, strip.white=TRUE)IFsource <- read.table("sampleNumIFsource.csv", sep="\t", header=TRUE, strip.white=TRUE)OFdest <- read.table("sampleNumOFdest.csv", sep="\t", header=TRUE, strip.white=TRUE)OFsource <- read.table("sampleNumOFsource.csv", sep="\t", header=TRUE, strip.white=TRUE)# add an idix <- 1:nrow(plotData)plotData$id <- 1:nrow(plotData)plotData <- plotData[,c(5,1,2,3,4)]# combine the different dataframeplotData$IFdest <- c(IFdest$Freq, NA)plotData$IFsource <- c(IFsource$Freq, NA, NA)plotData$OFdest <- OFdest$FreqplotData$OFsource <- c(OFsource$Freq, NA, NA)# reshape the dataframelong <- cbind( melt(plotData, id = c("id"), measure = c(2:5), variable = "type", value.name = "value"), melt(plotData, id = c("id"), measure = c(6:9), variable = "name", value.name = "numbers"))long <- long[,-c(4,5)] # this removes two unneceassary columnsWhen you have done that, you can use geom_text to plot the numbers on top of the bars:# create your plotggplot(long, aes(x = id, y = value, fill = type)) + geom_bar(stat = "identity", position = "dodge") + geom_text(aes(label = numbers), vjust=-1, position = position_dodge(0.9), size = 3) + scale_x_continuous(breaks = ix) + labs(x = "Nodes", y = "Throughput (Mbps)") + scale_fill_discrete(name="Legend", labels=c("Inside Firewall (Dest)", "Inside Firewall (Source)", "Outside Firewall (Dest)", "Outside Firewall (Source)")) + theme_bw() + theme(legend.position="right") + theme(legend.title = element_text(colour="blue", size=14, face="bold")) + theme(legend.text = element_text(colour="blue", size=12, face="bold"))The result:As you can see, the text labels overlap sometimes. You can change that by decreasing the size of the text, but then you run the risk that the labels become hard to read. You might therefore consider to use facets by adding facet_grid(type ~ .) (or facet_wrap(~ type)) to the plotting code:ggplot(long, aes(x = id, y = value, fill = type)) + geom_bar(stat = "identity", position = "dodge") + geom_text(aes(label = numbers), vjust=-0.5, position = position_dodge(0.9), size = 3) + scale_x_continuous("Nodes", breaks = ix) + scale_y_continuous("Throughput (Mbps)", limits = c(0,1000)) + scale_fill_discrete(name="Legend", labels=c("Inside Firewall (Dest)", "Inside Firewall (Source)", "Outside Firewall (Dest)", "Outside Firewall (Source)")) + theme_bw() + theme(legend.position="right") + theme(legend.title = element_text(colour="blue", size=14, face="bold")) + theme(legend.text = element_text(colour="blue", size=12, face="bold")) + facet_grid(type ~ .)which results in the following plot: 这篇关于如何使用R中的ggplot / geom_bar从数据集的顶部添加自定义标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-19 03:40