我有下面的資料框并創建一個plotly帶有內圈的繪圖物件。我的問題是我想跳過value灰點和綠點。
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"))
uj5u.com熱心網友回復:
我洗掉了過濾器,以便繪制點。如果您想在所有點中都使用相同的工具提示,您可以text將ggplot(aes()). 如果您仍然想要點,percent則darkolivegreen需要將 添加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適當點的值,請使用此選項。順便說一句,我已將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")

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/437582.html
上一篇:如何按數字順序放置圖例名稱
