如何调整 plotly hovermode 'x' 选项以在没有额外美学和重叠坐标的情况下工作?
How to adapt plotly hovermode 'x' option to work with no additional aesthetics and at overlapping co-ordinates?
我正在尝试复制在 this 33.1 处的情节示例中看到的悬停模式:
它只提供悬停时特定 x 坐标的信息,但有多个条目。
在我的真实数据集中,除了 text
之外,我没有设置任何额外的美学,因为我想在悬停中获得一些额外的信息。我的数据集有一些 x 坐标的多个条目,如示例中所示。但是,当我尝试为我的真实数据集调整代码时:
p <- ggplot(df, mapping = aes(x, Points, text = paste("Person: ", Person,
"\nEvent: ", Event))) +
# geom_jitter(shape = 4, width = 0, height = 50) +
geom_point(shape = 4) +
ggplotly(p) %>%
layout(hovermode = "x")
特定 x 坐标的悬停现在只显示一个人。我试过垂直抖动这些点,看看它是否会有所作为,因为我读到重叠点经常有问题,但这并不能解决我的问题。
我尝试设置颜色美学,只是为了测试它的外观:
p <- ggplot(df, mapping = aes(x, Points, colour = Event, text = paste("Person: ", Person,
"\nEvent: ", Event))) +
geom_point(shape = 4)
ggplotly(p) %>%
layout(hovermode = "x")
我可以很容易地摆脱图例并使所有颜色都设置为单一颜色,但在悬停时它显示来自多个 x 坐标的值,这引入了另一个问题。
x 坐标 1090 处的期望输出:
我的 df:
df <- structure(list(Event = c("2012", "2008", "2005", "2012", "2018", "2005", "2009", "2018", "2013", "2016", "2007", "2000", "2017", "2000", "2000", "2015", "2004", "2012", "2002", "2015", "2017", "2016", "2018", "2013", "2005", "2015", "2001", "2000", "2008", "2004", "2012", "2003", "2016", "2015", "2007", "2009", "2006", "2011", "2016", "2001", "2018", "2010", "2011", "2016", "2005", "2017", "2004", "2004", "2009", "2004", "2015", "2002", "2012", "2001", "2006", "2004", "2014", "2012", "2003", "2008", "2007", "2015", "2009", "2011"), x = c(1106, 1124, 1090, 1110, 1090, 1107, 1066, 1112, 1098, 1096, 1121, 1121, 1094, 1121, 1131, 1105, 1106, 1109, 1083, 1097, 1093, 1062, 1082, 1105, 1067, 1122, 1099, 1075, 1113, 1108, 1090, 1072, 1130, 1127, 1086, 1089, 1114, 1111, 1117, 1072, 1129, 1135, 1128, 1086, 1081, 1126, 1086, 1050, 1093, 1055, 1126, 1089, 1035, 1074, 1083, 1066, 1050, 1078, 1075, 1115, 1104, 1093, 1088, 1125), Points = c(847, 808, 883, 838, 883, 845, 938, 834, 865, 870, 814, 814, 874, 814, 793, 850, 847, 841, 899, 867, 876, 947, 901, 850, 935, 812, 863, 917, 832, 843, 883, 924, 795, 801, 892, 885, 830, 836, 823, 924, 797, 784, 799, 892, 903, 804, 892, 975, 876, 963, 804, 885, 1011, 919, 899, 938, 975, 910, 917, 827, 852, 876, 888, 806), Person = c("Pascal", "Hans", "Attila", "Brent", "Fredrik", "Óscar", "Yunior", "Pieter", "Sergey", "Adam", "Nicklas", "Fedor", "Marek", "Michael", "Indrek", "Simone", "Qi", "Willem", "Eugenio", "Pieter", "Devon", "Kai", "Martin", "Adam", "Hamdi", "Bastien", "Eduard", "Lev", "Aleksey", "Indrek", "Norman", "Dmitriy", "Keisuke", "Keisuke", "Andrei", "Eelco", "Andrei", "Kim", "Jonas", "Dean", "Niklas", "Petter", "Mikk", "Cedric", "Christopher", "Gael", "Markus", "Dmitriy", "Norman", "Nikolay", "Pau", "Thomas", "Ashton", "Lev", "Maurice", "Chiel", "Trey", "Sergey", "Markus", "Mikk", "Roman", "Ingmar", "Pascal", "Roman")), row.names = c(NA, -64L), class = "data.frame")
尝试修复悬停距离
p <- ggplot(df, mapping = aes(x, Points, colour = Event, text = paste("Person: ", Person,
"\nEvent: ", Event))) +
theme(legend.position='none')+
geom_point(shape = 4)
ggplotly(p) %>%
layout(hovermode = "x", hoverdistance = 1)
我正在尝试复制在 this 33.1 处的情节示例中看到的悬停模式:
它只提供悬停时特定 x 坐标的信息,但有多个条目。
在我的真实数据集中,除了 text
之外,我没有设置任何额外的美学,因为我想在悬停中获得一些额外的信息。我的数据集有一些 x 坐标的多个条目,如示例中所示。但是,当我尝试为我的真实数据集调整代码时:
p <- ggplot(df, mapping = aes(x, Points, text = paste("Person: ", Person,
"\nEvent: ", Event))) +
# geom_jitter(shape = 4, width = 0, height = 50) +
geom_point(shape = 4) +
ggplotly(p) %>%
layout(hovermode = "x")
特定 x 坐标的悬停现在只显示一个人。我试过垂直抖动这些点,看看它是否会有所作为,因为我读到重叠点经常有问题,但这并不能解决我的问题。
我尝试设置颜色美学,只是为了测试它的外观:
p <- ggplot(df, mapping = aes(x, Points, colour = Event, text = paste("Person: ", Person,
"\nEvent: ", Event))) +
geom_point(shape = 4)
ggplotly(p) %>%
layout(hovermode = "x")
我可以很容易地摆脱图例并使所有颜色都设置为单一颜色,但在悬停时它显示来自多个 x 坐标的值,这引入了另一个问题。
x 坐标 1090 处的期望输出:
我的 df:
df <- structure(list(Event = c("2012", "2008", "2005", "2012", "2018", "2005", "2009", "2018", "2013", "2016", "2007", "2000", "2017", "2000", "2000", "2015", "2004", "2012", "2002", "2015", "2017", "2016", "2018", "2013", "2005", "2015", "2001", "2000", "2008", "2004", "2012", "2003", "2016", "2015", "2007", "2009", "2006", "2011", "2016", "2001", "2018", "2010", "2011", "2016", "2005", "2017", "2004", "2004", "2009", "2004", "2015", "2002", "2012", "2001", "2006", "2004", "2014", "2012", "2003", "2008", "2007", "2015", "2009", "2011"), x = c(1106, 1124, 1090, 1110, 1090, 1107, 1066, 1112, 1098, 1096, 1121, 1121, 1094, 1121, 1131, 1105, 1106, 1109, 1083, 1097, 1093, 1062, 1082, 1105, 1067, 1122, 1099, 1075, 1113, 1108, 1090, 1072, 1130, 1127, 1086, 1089, 1114, 1111, 1117, 1072, 1129, 1135, 1128, 1086, 1081, 1126, 1086, 1050, 1093, 1055, 1126, 1089, 1035, 1074, 1083, 1066, 1050, 1078, 1075, 1115, 1104, 1093, 1088, 1125), Points = c(847, 808, 883, 838, 883, 845, 938, 834, 865, 870, 814, 814, 874, 814, 793, 850, 847, 841, 899, 867, 876, 947, 901, 850, 935, 812, 863, 917, 832, 843, 883, 924, 795, 801, 892, 885, 830, 836, 823, 924, 797, 784, 799, 892, 903, 804, 892, 975, 876, 963, 804, 885, 1011, 919, 899, 938, 975, 910, 917, 827, 852, 876, 888, 806), Person = c("Pascal", "Hans", "Attila", "Brent", "Fredrik", "Óscar", "Yunior", "Pieter", "Sergey", "Adam", "Nicklas", "Fedor", "Marek", "Michael", "Indrek", "Simone", "Qi", "Willem", "Eugenio", "Pieter", "Devon", "Kai", "Martin", "Adam", "Hamdi", "Bastien", "Eduard", "Lev", "Aleksey", "Indrek", "Norman", "Dmitriy", "Keisuke", "Keisuke", "Andrei", "Eelco", "Andrei", "Kim", "Jonas", "Dean", "Niklas", "Petter", "Mikk", "Cedric", "Christopher", "Gael", "Markus", "Dmitriy", "Norman", "Nikolay", "Pau", "Thomas", "Ashton", "Lev", "Maurice", "Chiel", "Trey", "Sergey", "Markus", "Mikk", "Roman", "Ingmar", "Pascal", "Roman")), row.names = c(NA, -64L), class = "data.frame")
尝试修复悬停距离
p <- ggplot(df, mapping = aes(x, Points, colour = Event, text = paste("Person: ", Person,
"\nEvent: ", Event))) +
theme(legend.position='none')+
geom_point(shape = 4)
ggplotly(p) %>%
layout(hovermode = "x", hoverdistance = 1)