gpt4 book ai didi

R:在 ggplot2 图中按值而不是按字母顺序对方面进行排序

转载 作者:行者123 更新时间:2023-12-04 02:36:32 24 4
gpt4 key购买 nike

几周前,我使用 ggplot2 创建了一个分面图,其中分面按数据框中的最后一个值排序。在重新排序之前,我没有遇到任何重大问题,因为我还没有真正理解订单、因素和级别的所有复杂情况。尽管如此,在引用 SO 帖子一两个(或三个)小时后,我得到了它的工作。

当我今天回到脚本时,它不再“工作”,因为它现在按字母顺序而不是按数据框的最终值对构面进行排序。 (我认为我最初“修复”了这个问题,同时在 R 控制台上乱搞,实际上并没有将解决方案添加到脚本中。)今晚我不想再花几个小时在这个问题上,而是让自己任凭摆布SO的。

问:如何按指定值而不是按每个方面名称的字母顺序对方面进行排序?请注意以下代码仅为示例;真实数据有几十项。

编辑下面的代码以反射(reflect)来自@joran 的其他输入;现在,这些方面已被适本地排序和填充。任务成功。

# Version 3
require(ggplot2) ## NB This script assumes you have ggplot2 v0.90
require(scales)
require(plyr)
require(lubridate)
require(reshape)

set.seed(12345)
monthsback <- 15
date <- as.Date(paste(year(now()),month(now()),"1",sep="-")) - months(monthsback)
myitems <- data.frame(mydate=seq(as.Date(date), by="month", length.out=monthsback),
aaa = runif(monthsback, min = 600, max = 800),
bbb = runif(monthsback, min = 100, max = 200),
ccc = runif(monthsback, min = 1400, max = 2000),
ddd = runif(monthsback, min = 50, max = 120))

myitems <- melt(myitems, id = c('mydate'))

change_from_start <- function(x) {
(x - x[1]) / x[1]
}

myitems <- ddply(myitems, .(variable), transform, value = change_from_start(value))
myitems$mydate <- as.Date(myitems$mydate, format = "%Y-%m-%d")
myvals <- myitems[myitems$mydate == myitems$mydate[nrow(myitems)],] # get values on which to sort facets
myvals <- within(myvals, variable <- factor(variable, as.character(myvals[order(myvals$value, decreasing = T),]$variable),ordered = TRUE))
myitems <- within(myitems, variable <- factor(variable, as.character(myvals[order(myvals$value, decreasing = T),]$variable),ordered = TRUE))
print(levels(myitems$variable)) # check to see if ordering succeeded
myitems$fill <- ifelse(myitems$variable == "ddd", "blue", "darkgreen")

p <- ggplot(myitems, aes(y = value, x = mydate, group = variable)) +
geom_rect(aes(xmin = as.Date(myitems$mydate[1]), xmax = Inf, fill = fill), ymin = -Inf, ymax = Inf) +
scale_fill_manual(values = c("blue", "darkgreen")) +
geom_line(colour = "black") +
geom_text(data = myvals, aes(x = as.Date(myitems$mydate[1]) + 250, y = 0.2, label = sprintf("%1.1f%%", value * 100))) +
facet_wrap( ~ variable, ncol = 2) +
geom_hline(yintercept = 0, size = 0.6, linetype = "dotdash") +
scale_y_continuous(label = percent_format()) +
scale_x_date(expand = c(0,0), labels = date_format("%Y-%m"), breaks = date_breaks("year")) +
xlab(NULL) +
ylab(NULL) +
opts(legend.position = "none") +
opts(panel.grid.minor = theme_blank()) +
opts()

print(p)

Image showing that facets are now sorted properly but that the fill is no longer working

最佳答案

你有两个问题:

  • 转换 myitems$variable 的行一个因素应该指定 ordered = TRUE ,以确保它将是一个有序的因素。
  • 您的 geom_text call 使用一个单独的数据框,其对应的变量不是一个因子(或有序的),所以它践踏了 myitems 中的一个的有序性质。 .

  • 转换它们或有序因子,你应该没问题。

    关于R:在 ggplot2 图中按值而不是按字母顺序对方面进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654982/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com