Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplotly设置工具提示参数时不打印几何图形线_R_Ggplot2_Ggplotly - Fatal编程技术网

R 使用ggplotly设置工具提示参数时不打印几何图形线

R 使用ggplotly设置工具提示参数时不打印几何图形线,r,ggplot2,ggplotly,R,Ggplot2,Ggplotly,我试图用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:", r

我试图用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"))
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"))
dput(夏季)如下所示:

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"))
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"))

请帮忙

您需要在geoms中使用
标签
,而不是
文本

但是,在使用
plotly::ggplotly
生成生产质量图时,您可能会遇到一些挑战(正如我所遇到的),它还不太成熟,我发现定位和标签方面的问题还没有完全准备好进入黄金时间

替代溶液 此外,我还提供了完整的plotly解决方案,它绕过了
plotly::ggplotly
方法中的缺点。在下面绘制输出,并在底部绘制以下两个的可再现源

可复制源
library(plotly)
sumsd%
添加跟踪(
x=~年,
y=~中位数,
行=列表(颜色=“红色”),
标记=列表(颜色=“红色”),
hoverinfo=“text”,
text=~粘贴(
“年:”,年,“
”, “平均值:”,平均值,
“, “所需收入中位数:”,四舍五入(medianir,2) ), showlegend=FALSE, name=“中值”, 数据=sumsd ) %>% 添加跟踪( x=~年, y=~我的意思是, 行=列表(颜色=“绿色”), 标记=列表(颜色=“绿色”), hoverinfo=“text”, text=~粘贴( “年:”,年,“
”, “平均值:”,平均值,
“, “所需收入平均数:”,四舍五入(平均数,2) ), showlegend=FALSE, name=“BMR Mean”, 数据=sumbmr ) %>% 添加跟踪( x=~年, y=~中位数, 行=列表(color=“橙色”), 标记=列表(颜色=“橙色”), hoverinfo=“text”, text=~粘贴( “年:”,年,“
”, “平均值:”,平均值,
“, “所需收入中位数:”,四舍五入(medianir,2) ), showlegend=FALSE, name=“BMR中值”, 数据=sumbmr ) %>% 布局( xaxis=列表(title=“”), yaxis=列表(title=“可承受性(%)”) )
我找到的解决方案是添加一个名为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 (%)') 

一年来,我在使用
ggplotly
函数时遇到了很多问题,有时使用开发版本会有所帮助,但还远远不够完美。如果您真的想要交互性,那么最好完全进入
plotly
。如果你能接受的话,我很乐意提供一个使用
plot\ly
的解决方案。事实上,我能够找到另一个使用组的解决方案,我会将其作为另一个答案发布,但这同样有效。我会接受这个最佳答案。谢谢你!