1

我正在尝试使用 ggplot 进行绘图,它工作得非常好,但是当我添加工具提示修饰符时,它会破坏 ggplot。这是ggplot的代码:

ggplot() +
      # mean in blue, must have label in both geom_point and geom_line to show up??
      geom_point(data = sumsd, aes(x = Year, y = Mean, text = paste("Income Required Mean:", round(meanir, 2))), color = "blue") +
      geom_line(data = sumsd, aes(x = Year, y = Mean, text = paste("Income Required Mean:", round(meanir, 2))), color = "blue") +

      # median in red
      geom_point(data = sumsd, aes(x = Year, y = Median, text = paste("Income Required Median:", round(medianir, 2))), color = "red") +
      geom_line(data = sumsd, aes(x = Year, y = Median, text = paste("Income Required Median:", round(medianir, 2))), color = "red") +

      # BMR Mean in green
      geom_point(data = sumbmr, aes(x = sumbmr$year, y = BMRMean, text = paste("Income Required Mean:", round(meanir, 2))), color = "green") +
      geom_line(data = sumbmr, aes(x = sumbmr$year, y = BMRMean, text = paste("Income Required Mean:", round(meanir, 2))), color = "green") +

      # BMR Median in Orange
      geom_point(data = sumbmr, aes(x = sumbmr$year, y = BMRMedian, text = paste("Income Required Median:", round(medianir, 2))), color = "orange")  +
      geom_line(data = sumbmr, aes(x = sumbmr$year, y = BMRMedian, text = paste("Income Required Median:", round(medianir, 2))), color = "orange") +

      xlab('Date') +
      ylab('Affordability (%)')  

当您绘制此图时,不仅线条不起作用,而且所有内容都加倍。

在此处输入图像描述

删除“text =”部分的那一刻,它工作得非常好。

在此处输入图像描述

我正在尝试使用其他 SO 问题的解决方案,但问题是我的情况是独一无二的,因为我是从两个不同的数据帧中绘制的。我在想一种解决方案是组合数据框并使用组,但我不确定在这种情况下如何正确使用组。

dput(sumsd) 看起来像这样:

structure(list(year = c(1998, 1999, 2000, 2001, 2002, 2003, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
2016, 2017), Mean = c(93.7169512394609, 99.2423930840141, 121.481331406585, 
126.775399601736, 112.466413730831, 103.986381539689, 122.373342680171, 
150.048597477934, 160.564554366089, 155.500390025812, 125.850336953549, 
103.448554350041, 107.25827986955, 100.606707776528, 105.998796554414, 
122.709555550239, 136.83930367275, 152.282460118587, 160.071088660241, 
154.14808421977), Median = c(85.4917695475731, 91.2054228882549, 
109.098207512634, 110.406161651376, 104.009081592691, 97.4604892752744, 
115.880140364587, 136.170512471096, 143.124069920242, 146.579045544699, 
116.696166130696, 96.5748315397845, 99.3163207081357, 89.8676706522828, 
97.8773119273362, 117.628144815457, 124.999783492508, 139.339479286517, 
147.437503023617, 142.066431589906), meanir = c(75858.7132682999, 
86152.0494420347, 111134.855904496, 115687.697423248, 113855.557186688, 
115393.107622111, 135534.013451155, 164724.526543259, 177908.848180357, 
174125.940380747, 140992.212450383, 116111.239666518, 117619.195974371, 
110479.184428429, 118090.047869615, 135916.239598085, 152767.827229155, 
172914.018516023, 181629.609510369, 184693.920051047), medianir = c(70291.4599746587, 
80490.2933468334, 98809.4765667835, 99746.8318817087, 103924.176219962, 
107999.634110941, 129599.560933129, 150791.941966219, 160980.586693667, 
163018.315639156, 129905.220274953, 105961.905165452, 107490.201874569, 
97301.5571471213, 108610.952794588, 129799.258369787, 138565.568293283, 
158535.311959079, 165056.044584646, 169131.502475625)), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("year", "Mean", "Median", "meanir", 
"medianir"), row.names = c(NA, -20L), na.action = structure(11492L, .Names = "11492", class = "omit"))

dput(summer) 看起来像这样:

structure(list(year = c(1998, 1999, 2000, 2001, 2002, 2003, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
2016, 2017), Mean = c(35.5975039642586, 40.4455188619846, 35.7695849667396, 
37.1825436379744, 45.4697841345466, 35.71278205506, 32.8176166454225, 
34.8607395091664, 47.9769662900546, 40.1449272824002, 62.0633765342318, 
73.5812934677564, 61.3025427925627, 65.1843431889166, 63.0657096256395, 
61.9985219499782, 73.9313965758768, 59.0247038824931, 61.6204148196363, 
67.2245622451913), Median = c(33.3902476171523, 33.398651550511, 
32.6622247899155, 33.1914067114214, 33.680900044327, 29.3944814757642, 
29.4066908397235, 34.1874903650837, 37.0654203449693, 36.8544480044624, 
65.1026513371994, 80.6104753594162, 63.4793583608239, 65.3828352952037, 
65.2586468365118, 60.3592697123653, 68.8351812023184, 50.305834291634, 
59.2415687112354, 69.2928997343465), meanir = c(28551.9072419551, 
35059.1694067028, 33357.0651434246, 32833.0548035115, 45225.3426313333, 
39719.5872679614, 36100.1651757794, 38597.8771598172, 52338.4936958301, 
45002.9992336611, 62802.4596860642, 73434.1474408398, 59915.0792794635, 
64350.8687798733, 61140.2935901313, 63757.2783380522, 82978.0660506505, 
64859.9174437382, 67598.7321700551, 72403.9056481494), medianir = c(26337.6466204512, 
29791.5971830558, 30686.1601901257, 29831.1291245983, 33724.4140478504, 
32395.8147753913, 33497.2072704284, 38716.8499642996, 39495.2624214766, 
41977.2162770827, 62018.1785695242, 76539.6463537657, 63807.6102430058, 
66778.9247048712, 61669.4212605037, 61720.3644841409, 78437.6889800418, 
57270.3720129811, 59623.9489450214, 62814.0136091851)), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("year", "Mean", "Median", "meanir", 
"medianir"), row.names = c(NA, -20L), na.action = structure(c(12L, 
13L, 14L, 15L, 16L, 17L, 18L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 
81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 
94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 
117L, 118L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L, 
259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L, 
270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L, 
281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L, 
292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L, 
303L, 304L, 305L, 306L, 307L, 308L, 309L, 310L, 311L, 312L), .Names = c("12", 
"13", "14", "15", "16", "17", "18", "61", "62", "63", "64", "65", 
"66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", 
"77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", 
"88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", 
"99", "100", "101", "102", "103", "104", "105", "106", "107", 
"108", "109", "110", "111", "112", "113", "114", "115", "116", 
"117", "118", "250", "251", "252", "253", "254", "255", "256", 
"257", "258", "259", "260", "261", "262", "263", "264", "265", 
"266", "267", "268", "269", "270", "271", "272", "273", "274", 
"275", "276", "277", "278", "279", "280", "281", "282", "283", 
"284", "285", "286", "287", "288", "289", "290", "291", "292", 
"293", "294", "295", "296", "297", "298", "299", "300", "301", 
"302", "303", "304", "305", "306", "307", "308", "309", "310", 
"311", "312"), class = "omit"))

请帮忙!

4

2 回答 2

2

你需要使用label而不是text在你的geoms中。

在此处输入图像描述

但是,您可能会(就像我一样)在使用 制作生产质量的地块时遇到挑战plotly::ggplotly,它还不是很成熟,而且我发现定位和标签的问题还没有为黄金时段做好准备。

替代解决方案

此外,我提供了一个完整的情节解决方案,它规避了该plotly::ggplotly方法的缺点。在下方绘制输出,在底部绘制两者的可重复来源。

在此处输入图像描述

可重现的来源

library(plotly)

sumsd <- structure(

  list(
    year = c(
      1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
      2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
    ),
    Mean = c(
      93.7169512394609, 99.2423930840141, 121.481331406585, 126.775399601736,
      112.466413730831, 103.986381539689, 122.373342680171, 150.048597477934,
      160.564554366089, 155.500390025812, 125.850336953549, 103.448554350041,
      107.25827986955, 100.606707776528, 105.998796554414, 122.709555550239,
      136.83930367275, 152.282460118587, 160.071088660241, 154.14808421977
    ),
    Median = c(
      85.4917695475731, 91.2054228882549, 109.098207512634, 110.406161651376,
      104.009081592691, 97.4604892752744, 115.880140364587, 136.170512471096,
      143.124069920242, 146.579045544699, 116.696166130696, 96.5748315397845,
      99.3163207081357, 89.8676706522828, 97.8773119273362, 117.628144815457,
      124.999783492508, 139.339479286517, 147.437503023617, 142.066431589906
    ),
    meanir = c(
      75858.7132682999, 86152.0494420347, 111134.855904496, 115687.697423248,
      113855.557186688, 115393.107622111, 135534.013451155, 164724.526543259,
      177908.848180357, 174125.940380747, 140992.212450383, 116111.239666518,
      117619.195974371, 110479.184428429, 118090.047869615, 135916.239598085,
      152767.827229155, 172914.018516023, 181629.609510369, 184693.920051047
    ),
    medianir = c(
      70291.4599746587, 80490.2933468334, 98809.4765667835, 99746.8318817087,
      103924.176219962, 107999.634110941, 129599.560933129, 150791.941966219,
      160980.586693667, 163018.315639156, 129905.220274953, 105961.905165452,
      107490.201874569, 97301.5571471213, 108610.952794588, 129799.258369787,
      138565.568293283, 158535.311959079, 165056.044584646, 169131.502475625
    )
  ),
  class = c("tbl_df", "tbl", "data.frame"),
  .Names = c("year", "Mean", "Median", "meanir", "medianir"),
  row.names = c(NA, -20L),
  na.action = structure(11492L, .Names = "11492", class = "omit")

)

sumbmr <- structure(

  list(
    year = c(
      1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
      2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
    ),
    Mean = c(
      35.5975039642586, 40.4455188619846, 35.7695849667396, 37.1825436379744,
      45.4697841345466, 35.71278205506, 32.8176166454225, 34.8607395091664,
      47.9769662900546, 40.1449272824002, 62.0633765342318, 73.5812934677564,
      61.3025427925627, 65.1843431889166, 63.0657096256395, 61.9985219499782,
      73.9313965758768, 59.0247038824931, 61.6204148196363, 67.2245622451913
    ),
    Median = c(
      33.3902476171523, 33.398651550511, 32.6622247899155, 33.1914067114214,
      33.680900044327, 29.3944814757642, 29.4066908397235, 34.1874903650837,
      37.0654203449693, 36.8544480044624, 65.1026513371994, 80.6104753594162,
      63.4793583608239, 65.3828352952037, 65.2586468365118, 60.3592697123653,
      68.8351812023184, 50.305834291634, 59.2415687112354, 69.2928997343465
    ),
    meanir = c(
      28551.9072419551, 35059.1694067028, 33357.0651434246, 32833.0548035115,
      45225.3426313333, 39719.5872679614, 36100.1651757794, 38597.8771598172,
      52338.4936958301, 45002.9992336611, 62802.4596860642, 73434.1474408398,
      59915.0792794635, 64350.8687798733, 61140.2935901313, 63757.2783380522,
      82978.0660506505, 64859.9174437382, 67598.7321700551, 72403.9056481494
    ),
    medianir = c(
      26337.6466204512, 29791.5971830558, 30686.1601901257, 29831.1291245983,
      33724.4140478504, 32395.8147753913, 33497.2072704284, 38716.8499642996,
      39495.2624214766, 41977.2162770827, 62018.1785695242, 76539.6463537657,
      63807.6102430058, 66778.9247048712, 61669.4212605037, 61720.3644841409,
      78437.6889800418, 57270.3720129811, 59623.9489450214, 62814.0136091851
    )
  ),
  class = c("tbl_df", "tbl", "data.frame"),
  .Names = c("year", "Mean", "Median", "meanir", "medianir"),
  row.names = c(NA, -20L),
  na.action = structure(
    c(
      12L, 13L, 14L, 15L, 16L, 17L, 18L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
      68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L,
      82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L,
      96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L,
      108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 250L,
      251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L,
      263L, 264L, 265L, 266L, 267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L,
      275L, 276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L,
      287L, 288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L,
      299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L, 310L,
      311L, 312L
    ),
    .Names = c(
      "12", "13", "14", "15", "16", "17", "18", "61", "62", "63", "64", "65",
      "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
      "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
      "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101",
      "102", "103", "104", "105", "106", "107", "108", "109", "110", "111",
      "112", "113", "114", "115", "116", "117", "118", "250", "251", "252",
      "253", "254", "255", "256", "257", "258", "259", "260", "261", "262",
      "263", "264", "265", "266", "267", "268", "269", "270", "271", "272",
      "273", "274", "275", "276", "277", "278", "279", "280", "281", "282",
      "283", "284", "285", "286", "287", "288", "289", "290", "291", "292",
      "293", "294", "295", "296", "297", "298", "299", "300", "301", "302",
      "303", "304", "305", "306", "307", "308", "309", "310", "311", "312"
    ),
    class = "omit"
  )

)

# ggplotly solution

sumsd <- sumsd %>%
  mutate(
    `Income Required Mean` = round(meanir, 2),
    `Income Required Median` = round(medianir, 2)
  )

sumbmr <- sumbmr %>%
  mutate(
    `Income Required Mean` = round(meanir, 2),
    `Income Required Median` = round(medianir, 2)
  )

gg <- ggplot() +
  # mean in blue, must have label in both geom_point and geom_line to show up??
  geom_point(data = sumsd, aes(x = year, y = Mean, label = `Income Required Mean`), color = "blue") +
  geom_line(data = sumsd, aes(x = year, y = Mean, label = `Income Required Mean`), color = "blue") +

  # median in red
  geom_point(data = sumsd, aes(x = year, y = Median, label = `Income Required Median`), color = "red") +
  geom_line(data = sumsd, aes(x = year, y = Median, label = `Income Required Median`), color = "red") +

  # BMR Mean in green
  geom_point(data = sumbmr, aes(x = year, y = Mean, label = `Income Required Mean`), color = "green") +
  geom_line(data = sumbmr, aes(x = year, y = Mean, label = `Income Required Mean`), color = "green") +

  # BMR Median in Orange
  geom_point(data = sumbmr, aes(x = year, y = Median, label = `Income Required Median`), color = "orange")  +
  geom_line(data = sumbmr, aes(x = year, y = Median, label = `Income Required Median`), color = "orange") +

  xlab('Date') +
  ylab('Affordability (%)')

ggplotly(gg)

# full plotly

plot_ly(type = "scatter", mode = "markers+lines") %>%
  add_trace(
    x = ~year,
    y = ~Mean,
    line = list(color = "blue"),
    marker = list(color = "blue"),
    hoverinfo = "text",
    text = ~paste(
      " Year: ", year, "<br>",
      "Mean: ", Mean, "<br>",
      "Income Required Mean: ", round(meanir, 2)
    ),
    showlegend = FALSE,
    name = "Mean",
    data = sumsd
  ) %>%
  add_trace(
    x = ~year,
    y = ~Median,
    line = list(color = "red"),
    marker = list(color = "red"),
    hoverinfo = "text",
    text = ~paste(
      " Year: ", year, "<br>",
      "Mean: ", Mean, "<br>",
      "Income Required Median: ", round(medianir, 2)
    ),
    showlegend = FALSE,
    name = "Median",
    data = sumsd
  ) %>%
  add_trace(
    x = ~year,
    y = ~Mean,
    line = list(color = "green"),
    marker = list(color = "green"),
    hoverinfo = "text",
    text = ~paste(
      " Year: ", year, "<br>",
      "Mean: ", Mean, "<br>",
      "Income Required Mean: ", round(meanir, 2)
    ),
    showlegend = FALSE,
    name = "BMR Mean",
    data = sumbmr
  ) %>%
  add_trace(
    x = ~year,
    y = ~Median,
    line = list(color = "orange"),
    marker = list(color = "orange"),
    hoverinfo = "text",
    text = ~paste(
      " Year: ", year, "<br>",
      "Mean: ", Mean, "<br>",
      "Income Required Median: ", round(medianir, 2)
    ),
    showlegend = FALSE,
    name = "BMR Median",
    data = sumbmr
  ) %>%
  layout(
    xaxis = list(title = ""),
    yaxis = list(title = "Affordability (%)")
  )
于 2017-09-28T08:13:05.917 回答
0

我找到的解决方案是添加一个名为 group 的列,并在 group 列中将第一个数据框编号为 ​​1,在 group 列中将第二个数据框编号为 ​​2。然后,我将两个数据框合并为一个,并在ggplot的美学中添加了“group = group”。这是代码:

Year <- sumsd$year
    Mean <- sumsd$Mean
    Median <- sumsd$Median
    sumbmr <- bmr %>% group_by(year)%>%summarise(Mean=mean(ipm), Median=median(ipm), meanir = mean(ir), medianir = median(ir))
    BMRMean <- sumbmr$Mean
    BMRMedian <- sumbmr$Median

    namevector4 <- c("group")
    sumbmr[,namevector4] <- 2

    sumsd$meanbmr = BMRMean
    sumsd$medianbmr = BMRMedian
    sumsd$meanbmrir = sumbmr$meanir
    sumsd$medianbmrir = sumbmr$medianir

ggplot(sumsd, group = group) +
      # mean in blue
      geom_point(aes(x = year, y = Mean, text = paste("Income Required Average:", round(meanir, 2))), group = 1, color = "blue") + 
      geom_line(aes(x = year, y = Mean, text = paste("Income Required Average:", round(meanir, 2))), group = 1, color = "blue") + 

      #median in red
      geom_point(aes(x = year, y = Median, text = paste("Income Required Median:", round(medianir, 2))), group = 2, color = "red") + 
      geom_line(aes(x = year, y = Median, text = paste("Income Required Median:", round(medianir, 2))), group = 2, color = "red") + 

      # BMR Mean in green
      geom_point(aes(x = year, y = meanbmr, text = paste("BMR Income Required Average:", round(meanbmrir, 2))), group = 3, color = "green") + 
      geom_line(aes(x = year, y = meanbmr, text = paste("BMR Income Required Average:", round(meanbmrir, 2))), group = 3, color = "green") + 

      # BMR Median in orange
      geom_point(aes(x = year, y = medianbmr, text = paste("BMR Income Required Median:", round(medianbmrir, 2))), group = 4, color = "orange") + 
      geom_line(aes(x = year, y = medianbmr, text = paste("BMR Income Required Median:", round(medianbmrir, 2))), group = 4, color = "orange") + 
      xlab('Date') +
      ylab('Affordability (%)') 

使用此代码后图表似乎运行良好

在此处输入图像描述

希望这会帮助某人

于 2017-09-29T18:19:26.683 回答