从具有两条迹线的绘图图表的悬停文本中隐藏特定变量

发布于 2025-01-11 05:08:10 字数 3378 浏览 0 评论 0原文

我有下面的数据框,并创建一个带有内圆的 plotly 绘图对象。我的问题是我想跳过灰点和绿点中的值。

i <- 1

f<-bt %>% filter(issue==unique(bt$issue)[1]) %>%
ggplot(aes(x=year2))+
  
  geom_point(aes(y=value,size=value),color="gray")+
  geom_point(aes(y=value,size=percent),color="darkolivegreen")+
  scale_x_continuous(n.breaks=14)+
  theme(legend.position = 'none',
        axis.title.y = element_blank(),
        axis.text.y = element_blank())+
  labs(x="")

p2<-ggplotly(f)

p2$x$data[[1]]$text <- str_replace_all(p2$x$data[[1]]$text, 
                                       "year2", "Year") %>% 
  str_replace_all(., fixed("percent"),
                  "Percentage of all signed PTAs covering NTIs")

p2$x$data[[2]]$text <- str_replace_all(p2$x$data[[2]]$text, 
                                       "year2", "Year") %>% 
  str_replace_all(., fixed("percent"),
                  "Percentage of all signed PTAs covering NTIs")

p2

我的数据:

bt<-structure(list(year2 = c(1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 
    1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 
    1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955), issue = c("air", 
    "anti_corruption", "anti_drug", "anti_terror", "biodiversity", 
    "chemical", "child", "contaminant", "cpr", "csr", "democracy", 
    "development", "eductation", "environmental_goods_services", 
    "ep", "esr", "fish_stock", "food_security", "forest", "gender", 
    "good_governance", "health", "human_trafficking", "labor_at", 
    "labor_at_child", "labor_at_equal_opportunity", "labor_at_forced", 
    "labor_at_health_and_safety", "labor_at_minimum_wage", "labor_at_unionize", 
    "labor_at_working_time", "labor_to", "migrantworker", "mineral", 
    "minority", "national_security", "region", "religion", "renewable_energy", 
    "rule_of_law", "social", "waste", "water", "air", "air", "anti_corruption", 
    "anti_corruption", "anti_drug", "anti_drug", "anti_terror", "anti_terror", 
    "biodiversity", "biodiversity", "chemical", "chemical", "child", 
    "child", "contaminant", "contaminant", "cpr", "cpr", "csr", "csr", 
    "democracy", "democracy", "development", "development", "eductation", 
    "eductation", "environmental_goods_services"), value = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 8, NA, NA, 8, NA, NA, 
    NA, NA, NA, NA, 8, NA, NA, NA, NA, NA, NA, NA, NA, NA, 8, NA, 
    NA, NA, 16, NA, NA, NA, NA, 8, NA, NA, NA, NA, 12, 1, NA, NA, 
    12, 1, NA, NA, NA, NA, NA, NA, NA, NA, 24, 2, NA, NA, 12, 1, 
    36, 3, NA, NA, NA), percent = c(NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 0.125, NA, NA, 0.125, NA, NA, NA, NA, NA, NA, 0.125, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.125, NA, NA, NA, 0.0625, 
    NA, NA, NA, NA, 0.125, NA, NA, NA, NA, 0.0833333333333333, 2, 
    NA, NA, 0.0833333333333333, 2, NA, NA, NA, NA, NA, NA, NA, NA, 
    0.0416666666666667, 1, NA, NA, 0.0833333333333333, 2, 0.0277777777777778, 
    0.666666666666667, NA, NA, NA)), row.names = c(NA, -70L), class = c("tbl_df", 
    "tbl", "data.frame"))

I have the dataframe below and create a plotly plot object with inner circles. My issue is that I want to skip the value from both the gray and the green dots.

i <- 1

f<-bt %>% filter(issue==unique(bt$issue)[1]) %>%
ggplot(aes(x=year2))+
  
  geom_point(aes(y=value,size=value),color="gray")+
  geom_point(aes(y=value,size=percent),color="darkolivegreen")+
  scale_x_continuous(n.breaks=14)+
  theme(legend.position = 'none',
        axis.title.y = element_blank(),
        axis.text.y = element_blank())+
  labs(x="")

p2<-ggplotly(f)

p2$x$data[[1]]$text <- str_replace_all(p2$x$data[[1]]$text, 
                                       "year2", "Year") %>% 
  str_replace_all(., fixed("percent"),
                  "Percentage of all signed PTAs covering NTIs")

p2$x$data[[2]]$text <- str_replace_all(p2$x$data[[2]]$text, 
                                       "year2", "Year") %>% 
  str_replace_all(., fixed("percent"),
                  "Percentage of all signed PTAs covering NTIs")

p2

My data:

bt<-structure(list(year2 = c(1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 1950, 
    1950, 1950, 1950, 1950, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 
    1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 
    1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955, 1955), issue = c("air", 
    "anti_corruption", "anti_drug", "anti_terror", "biodiversity", 
    "chemical", "child", "contaminant", "cpr", "csr", "democracy", 
    "development", "eductation", "environmental_goods_services", 
    "ep", "esr", "fish_stock", "food_security", "forest", "gender", 
    "good_governance", "health", "human_trafficking", "labor_at", 
    "labor_at_child", "labor_at_equal_opportunity", "labor_at_forced", 
    "labor_at_health_and_safety", "labor_at_minimum_wage", "labor_at_unionize", 
    "labor_at_working_time", "labor_to", "migrantworker", "mineral", 
    "minority", "national_security", "region", "religion", "renewable_energy", 
    "rule_of_law", "social", "waste", "water", "air", "air", "anti_corruption", 
    "anti_corruption", "anti_drug", "anti_drug", "anti_terror", "anti_terror", 
    "biodiversity", "biodiversity", "chemical", "chemical", "child", 
    "child", "contaminant", "contaminant", "cpr", "cpr", "csr", "csr", 
    "democracy", "democracy", "development", "development", "eductation", 
    "eductation", "environmental_goods_services"), value = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 8, NA, NA, 8, NA, NA, 
    NA, NA, NA, NA, 8, NA, NA, NA, NA, NA, NA, NA, NA, NA, 8, NA, 
    NA, NA, 16, NA, NA, NA, NA, 8, NA, NA, NA, NA, 12, 1, NA, NA, 
    12, 1, NA, NA, NA, NA, NA, NA, NA, NA, 24, 2, NA, NA, 12, 1, 
    36, 3, NA, NA, NA), percent = c(NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 0.125, NA, NA, 0.125, NA, NA, NA, NA, NA, NA, 0.125, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.125, NA, NA, NA, 0.0625, 
    NA, NA, NA, NA, 0.125, NA, NA, NA, NA, 0.0833333333333333, 2, 
    NA, NA, 0.0833333333333333, 2, NA, NA, NA, NA, NA, NA, NA, NA, 
    0.0416666666666667, 1, NA, NA, 0.0833333333333333, 2, 0.0277777777777778, 
    0.666666666666667, NA, NA, NA)), row.names = c(NA, -70L), class = c("tbl_df", 
    "tbl", "data.frame"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2025-01-18 05:08:10

我删除了过滤器,以便绘制点。如果您希望所有点都具有相同的工具提示,可以将文本添加到ggplot(aes())。如果您仍希望在 darkolivegreen 点中使用 percent,则需要将text 添加到图层中。

仅年份,并在 ggplot() 中包含文本:

f <- bt %>% # filter(issue == unique(bt$issue)[1]) %>%
  ggplot(aes(x = year2, text = paste0("Year: ", year2))) +
  geom_point(aes(y = value, size = value), color = "gray") +
  geom_point(aes(y = value, size = percent), color = "darkolivegreen") +
  scale_x_continuous(n.breaks = 14) +
  theme(legend.position  =  'none',
        axis.title.y = element_blank(),
        axis.text.y = element_blank()) +
  labs(x = "")

ggplotly(f, tooltip = "text")

如果您想要适当点上的百分比值,请使用此选项。顺便说一句,我已将 percent 数据格式化为百分比。我提供了两种不同的方法来做到这一点。其中一种方法已被注释掉。未注释掉的使用了 scale 包。两者都有效;当然,这取决于你。

f <- bt %>% # filter(issue == unique(bt$issue)[1]) %>%
  ggplot(aes(x = year2)) +
  geom_point(aes(y = value, size = value, text = paste0("Year: ", year2)),
             color = "gray") +
  geom_point(aes(y = value, size = percent,
                 # text = paste0("Year: ", year2, "\nPercent: ", round(percent, 3) * 100, "%")), 
                 text = paste0("Year: ", year2, "\nPercent: ", scales::percent(percent))), 
             color = "darkolivegreen") +
  scale_x_continuous(n.breaks = 14) +
  theme(legend.position  =  'none',
        axis.title.y = element_blank(),
        axis.text.y = element_blank()) +
  labs(x = "")

ggplotly(f, tooltip = "text")

输入图片此处描述在此处输入图像描述

I removed the filter so that points plotted. If you want the same tooltip in all points, you can add the text to ggplot(aes()). If you still wanted percent in the darkolivegreen points, you will need to add the text to the layers.

Just years, with text in ggplot():

f <- bt %>% # filter(issue == unique(bt$issue)[1]) %>%
  ggplot(aes(x = year2, text = paste0("Year: ", year2))) +
  geom_point(aes(y = value, size = value), color = "gray") +
  geom_point(aes(y = value, size = percent), color = "darkolivegreen") +
  scale_x_continuous(n.breaks = 14) +
  theme(legend.position  =  'none',
        axis.title.y = element_blank(),
        axis.text.y = element_blank()) +
  labs(x = "")

ggplotly(f, tooltip = "text")

If you want the values from percent on the appropriate points, use this option. By the way, I've formatted the percent data as percentages. I provided two different ways of doing this. One of the methods is commented out. The one that is not commented out uses the scale package. Both work; it's up to you, of course.

f <- bt %>% # filter(issue == unique(bt$issue)[1]) %>%
  ggplot(aes(x = year2)) +
  geom_point(aes(y = value, size = value, text = paste0("Year: ", year2)),
             color = "gray") +
  geom_point(aes(y = value, size = percent,
                 # text = paste0("Year: ", year2, "\nPercent: ", round(percent, 3) * 100, "%")), 
                 text = paste0("Year: ", year2, "\nPercent: ", scales::percent(percent))), 
             color = "darkolivegreen") +
  scale_x_continuous(n.breaks = 14) +
  theme(legend.position  =  'none',
        axis.title.y = element_blank(),
        axis.text.y = element_blank()) +
  labs(x = "")

ggplotly(f, tooltip = "text")

enter image description hereenter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文