本文介绍了当绘制使用ggplot中的数据子集的图层时,因子水平的原始顺序在图例中发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在R中的 ggplot2 图中控制图例中项目的顺序。我查了一些其他类似的问题,并发现了改变我正在绘制的因子变量的级别。我正在绘制4个月,12月,1月,7月和6月的数据。 如果我只是在所有月份执行一次绘图命令,在图例中排列的月份按照因子水平的顺序出现。不过,我需要在夏季(六月和七月)和冬季(十二月和一月)数据中有不同的闪避值。我用两个 geom_pointrange 命令来做到这一点。当我将它分成两步时,图例的顺序会回到字母顺序。您可以通过评论情节夏天或情节冬天命令来演示。 我可以更改哪些内容以保留图例中的因子水平顺序? 请忽略奇怪的测试数据 - 这种情节格式中的实际数据看起来很好。 #testdata hour avg_hou upper_ci< - avg_hou + sample((例如0.5,0.001),96,替换= TRUE) lower_ci Month testdata< - data.frame(Month,hour,avg_hou,lower_ci,upper_ci) testdata $ Month #basic plot setup plotx< - ggplot(testdata,aes( x =小时,y = avg_hou,ymin = lower_ci,ymax = upper_ci,color = Month,shape = Month)) plotx< - plotx + scale_color_manual(values = c(June=#FDB863, July=#E66101,December=#92C5DE,January=#0571B0)) #plot summer plotx< - plotx + geom_pointrange(data = testdata [testdata $ Month ==June| | testdata $ Month ==July,],size = 1,position = position_dodge(width = 0.3)) #plot winter plotx< - plotx + geom_pointrange(data = testdata [testdata $ Month ==December| testdata $ Month ==January,],size = 1,position = position_dodge(width = 0.6)) print(plotx) 解决方案另一种思考闪避的方法是从x值的偏移量基于组(在本例中为月)。因此,如果我们在您的原始数据中添加闪避(x-offset)列,则基于月: #您的原始示例数据#注意使用set.seed(...),所以随机数据是可重现的 set.seed(1)小时 lower_ci avg_hou upper_ci Month testdata< - data.frame(Month,小时,avg_hou,lower_ci,upper_ci) testdata $ Month< - factor(testdata $ Month,levels = c(June,July,December,January)) #添加偏移列dodge testdata $ dodge< - -2.5+(as.integer(testdata $ Month)) #创建ggplot对象和默认映射ggp ggp< - ggp + scale_color_manual(values = c(June=#FDB863,July=#E66101,December=#92C5DE $ b $绘制点范围 ggp + geom_pointrange(aes(x = hour + 0.2 * dodge),size = 1) 产生此: 这不需要 geom_blank(...)来维持缩放顺序,并且它不需要两次调用 geom_pointrange(...) I am trying to control the order of items in a legend in a ggplot2 plot in R. I looked up some other similar questions and found out about changing the order of the levels of the factor variable I am plotting. I am plotting data for 4 months, December, January, July, and June.If I just do one plot command for all the months, it works as expected with the months ordered in the legend appearing in the order of the levels of the factor. However, I need to have a different dodge value for the summer (June & July) and winter (Dec & Jan) data. I do this with two geom_pointrange commands. When I divide it into 2 steps, the order of the legend goes back to alphabetical. You can demonstrate by commenting out the "plot summer" or "plot winter" command.What can I change to keep my factor level order in the legend? Please ignore the odd looking test data - the real data looks fine in this plot format.#testdatahour <- rep(seq(from=1,to=24,by=1),4)avg_hou <- sample(seq(0,0.5,0.001),96,replace=TRUE)lower_ci <- avg_hou - sample(seq(0,0.05,0.001),96,replace=TRUE)upper_ci <- avg_hou + sample(seq(0,0.05,0.001),96,replace=TRUE)Month <- c(rep("December",24), rep("January",24), rep("June",24), rep("July",24))testdata <- data.frame(Month,hour,avg_hou,lower_ci,upper_ci)testdata$Month <- factor(alldata$Month,levels=c("June", "July", "December","January"))#basic plot setupplotx <- ggplot(testdata, aes(x = hour, y = avg_hou, ymin = lower_ci, ymax = upper_ci, color = Month, shape = Month))plotx <- plotx + scale_color_manual(values = c("June" = "#FDB863", "July" = "#E66101", "December" = "#92C5DE", "January" = "#0571B0"))#plot summerplotx <- plotx + geom_pointrange(data = testdata[testdata$Month == "June" | testdata$Month == "July",], size = 1, position=position_dodge(width=0.3)) #plot winterplotx <- plotx + geom_pointrange(data = testdata[testdata$Month == "December" | testdata$Month == "January",], size = 1, position=position_dodge(width=0.6))print(plotx) 解决方案 Another way to think about "dodge" is as an offset from the x-values based on group (in this case Month). So if we add a dodge (x-offset) column to your original data, based on month:# your original sample data# note the use of set.seed(...) so "random" data is reproducibleset.seed(1)hour <- rep(seq(from=1,to=24,by=1),4)avg_hou <- sample(seq(0,0.5,0.001),96,replace=TRUE)lower_ci <- avg_hou - sample(seq(0,0.05,0.001),96,replace=TRUE)upper_ci <- avg_hou + sample(seq(0,0.05,0.001),96,replace=TRUE)Month <- c(rep("December",24), rep("January",24), rep("June",24), rep("July",24))testdata <- data.frame(Month,hour,avg_hou,lower_ci,upper_ci)testdata$Month <- factor(testdata$Month,levels=c("June", "July", "December","January"))# add offset column for dodgetestdata$dodge <- -2.5+(as.integer(testdata$Month))# create ggplot object and default mappingsggp <- ggplot(testdata, aes(x=hour, y = avg_hou, ymin = lower_ci, ymax = upper_ci, color = Month, shape = Month))ggp <- ggp + scale_color_manual(values = c("June" = "#FDB863", "July" = "#E66101", "December" = "#92C5DE", "January" = "#0571B0"))# plot the point rangeggp + geom_pointrange(aes(x=hour+0.2*dodge), size=1)Produces this:This does not require geom_blank(...) to maintain the scale order, and it does not require two calls to geom_pointrange(...) 这篇关于当绘制使用ggplot中的数据子集的图层时,因子水平的原始顺序在图例中发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 14:59