gpt4 book ai didi

r - 如何用丝带和线条组合和修改ggplot2图例?

转载 作者:行者123 更新时间:2023-12-05 01:04:16 25 4
gpt4 key购买 nike

我对以下情节的图例有几个问题:
enter image description here

  • 丝带和线条被分成 2 个不同的图例,但我希望它们被合并:图例中的线条应该具有与具有相同名称的丝带相对应的背景填充(并且对于“官方税收”条目是透明的)。
  • 图例中的线条因颜色而异,但它们应因颜色和线型而异。

  • 看来我需要用 scale_manualguide_legend选项,但我所有的尝试都惨遭失败。

    这是创建绘图的代码。 plotDumping是绘制绘图的函数, updateData为绘图生成数据框,'updateLabels' 为绘图生成脚注。
    library(ggplot2)
    library(grid)
    library(gridExtra)
    library(scales)

    max_waste_volume <- 2000
    Illegal_dumping_fine_P <- 300000
    Illigal_landfilling_fine_P1 <- 500000
    Fine_probability_k <- 0.5
    Official_tax_Ta <- 600

    # mwv = max_waste_volume
    # P = Illegal_dumping_fine_P
    # P1 = Illigal_landfilling_fine_P1
    # k = Fine_probability_k
    # Ta = Official_tax_Ta


    updateData <- function(mwv, k, P1, P, Ta){

    # creates and(or) updates global data frame to provide data for the plot

    new_data <<- NULL
    new_data <<- as.data.frame(0:mwv)
    names(new_data) <<- 'V'
    new_data$IlD <<- k*P1/new_data$V
    new_data$IlD_fill <<- new_data$IlD
    new_data$IlD_fill[new_data$IlD_fill > Ta] <<- NA # we don't want ribbon to
    new_data$IlL <<- Ta-k*P/new_data$V
    }

    updateLabels <- function(k, P1, P, Ta){

    ### creates footnote caption for the plot

    prob <- paste('Fining probability = ', k, sep = '')
    landfilling_fine <- paste('Illegal landfilling fine = ', P1, sep = '')
    dumping_fine <- paste('Illegal dumping fine = ', P, sep = '')
    tax <- paste('Official tax = ', Ta, sep = '')
    note <<- paste(prob, landfilling_fine, sep = '; ')
    note <<- paste(note, dumping_fine, sep = '; ')
    note <<- paste(note, tax, sep = '; ')
    note
    }


    plotDumping <- function(mwv,
    P,
    P1,
    k,
    Ta){

    ### this function draws the plot

    # initialise plot data
    updateData(mwv, k, P1, P, Ta)
    updateLabels(k, P1, P, Ta)

    # draw the plot
    profit <- ggplot(data=new_data, aes(x = new_data$V)) +
    geom_ribbon(show_guide = T, alpha = 0.25, ymax = Ta,
    aes(ymin = new_data$IlD_fill,
    fill = "Illegal landfill owner's\nprofitable ratio\n")) +
    geom_ribbon(show_guide = F, alpha = 0.25, ymin = 0,
    aes(ymax = new_data$IlL,
    fill = "Waste owner's\nprofitable ratio")) +
    geom_line(data=new_data,
    aes(y = new_data$IlD, col = "Illegal landfill owner's\nprofitable ratio\n"),
    size = 1,
    linetype = 4) +
    geom_line(data=new_data,
    aes(y = new_data$IlL, col = "Waste owner's\nprofitable ratio"),
    size = 1,
    linetype = 5) +
    geom_line(y = Ta,
    aes(col = "Official tax"),
    size = 1.5,
    linetype = 1)+
    xlim(c(0, max(new_data$V)))+
    ylim(c(0, Ta*1.5))+
    theme(axis.text.x = element_text(angle=0, hjust = 0),
    axis.title = element_text(face = 'bold', size = 14),
    title = element_text(face = 'bold', size = 16),
    legend.position = 'right',
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    legend.key.width = unit(1, 'cm'))+
    labs(title="Profitable ratio between the volume \nof illegally disposed waste \nand costs of illegal waste disposure",
    x="Waste volume, cubic meters",
    y="Cost per cubic meter, RUB")

    # add a footnote about paramaters used for the current plot
    profit <- arrangeGrob(profit, sub = textGrob(note,
    x = 0,
    hjust = -0.1,
    vjust=0.1,
    gp = gpar(fontface = "italic", fontsize = 12)))

    # show plot
    print(profit)
    }

    # draw the plot
    plotDumping(max_waste_volume,
    Illegal_dumping_fine_P,
    Illigal_landfilling_fine_P1,
    Fine_probability_k,
    Official_tax_Ta)

    最佳答案

    这种情况下的一种解决方法是添加 geom_ribbon()也适用于 Official tax (使用值 Ta 作为 ymaxymin )。这将使两个图例具有相同的级别,并且它们将连接在一起。然后用 scale_fill_manual()您可以为 Official tax 设置填充值到 NA,然后在此级别填充的图例中将作为背景。

    + geom_ribbon(show_guide = F, alpha = 0.25, ymin = Ta,ymax=Ta, 
    aes(fill = "Official tax"))

    + scale_fill_manual(values=c("#F8766D",NA,"#00BFC4"))

    enter image description here

    附言不要使用 $aes()ggplot()函数(仅使用列名)。正如您已经写的 data=new_data aes() 中的所有变量在这个数据框中寻找。

    关于r - 如何用丝带和线条组合和修改ggplot2图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469275/

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