gpt4 book ai didi

r - 如何在 geom_point() 中反转大小

转载 作者:行者123 更新时间:2023-12-03 23:18:33 25 4
gpt4 key购买 nike

我想绘制正值和负值,它们的比例大小由点表示,使得 0 在这两种情况下最小,最大的是它们的大小。我想要负点大小,使得 0 最小,-5 最大,同时正值 0 最小,5 最大。像这张图片

enter image description here

我知道如果只有一个变量,例如 here,我可以反转并执行此操作.

但我有两个尺寸。

我的示例数据和代码如下。

dput(ddf)
structure(list(tr = c(0.192833333333335, 0.800378947368421, 2.28270774476556,
0.586631034482762, 0.217825000000001, -0.773253846153845, -0.459235294117648,
-0.0706142857142855, -0.24942, 11.1025623422045, -0.871375, -0.929300000000012,
-5.92639999999999, 3.87432045251869, -3.62026818181818, -2.49045555555556,
-0.730590000000007, 3.03523157894737, 1.04723333333333, -0.120474999999999
), Itr = c(0.192833333333335, 0.800378947368421, 2.28270774476556,
0.586631034482762, 0.217825000000001, NA, NA, NA, NA, 11.1025623422045,
NA, NA, NA, 3.87432045251869, NA, NA, NA, 3.03523157894737, 1.04723333333333,
NA), Dtr = c(NA, NA, NA, NA, NA, -0.773253846153845, -0.459235294117648,
-0.0706142857142855, -0.24942, NA, -0.871375, -0.929300000000012,
-5.92639999999999, NA, -3.62026818181818, -2.49045555555556,
-0.730590000000007, NA, NA, -0.120474999999999), td = c("Increasing",
"Increasing", "Increasing", "Increasing", "Increasing", "Decreasing",
"Decreasing", "Decreasing", "Decreasing", "Increasing", "Decreasing",
"Decreasing", "Decreasing", "Increasing", "Decreasing", "Decreasing",
"Decreasing", "Increasing", "Increasing", "Decreasing"), lat = c(-58.07,
-57.9, -56.74, -55.22, -55.43, -54.12, -54.62, -54.63, -54.48,
-54.25, -52.89, -54.05, -49.79, -49.82, -48.83, -48.73, -49.69,
-49.29, -48.89, -49.37), lon = c(130.82, 131.15, 131.67, 129.14,
127.71, 127.42, 126.9, 128.43, 128.33, 130.17, 125.77, 128.69,
123.2, 123.15, 124.05, 123.67, 125.03, 124.91, 124.97, 124.98
)), .Names = c("tr", "Itr", "Dtr", "td", "lat", "lon"), class = "data.frame", row.names = c(NA,
20L))

代码:
plt<-ggplot()+
geom_point(data = ddf,aes(x = lon, y = lat,size = Itr,shape=td),fill="Blue")+
geom_point(data = ddf,aes(x = lon, y = lat,size = Dtr,shape=td),fill="Green", show.legend = FALSE)+
scale_size_continuous(name="IDt",range=c(2,12))+
scale_shape_manual(name= "td",values=c(25,24),labels=c("Decreasing","Increasing"))
plt

最佳答案

我认为这个问题是关于建立一个发散的大小尺度,其中最接近 0 的值最小,离 0 最远的值最大。

为了实现这一点,您需要修改 ddf包括两个新的列( all_trs 结合了 DtrItrabs_trs 只是 all_trs 的绝对值,您将用于发散大小比例):

library(dplyr) library(magrittr)
ddf %<>% mutate(all_trs = ifelse(td == "Increasing", Itr, Dtr), abs_trs =abs(all_trs))

您显然可以按照基本方式构建这些 ddf$all_trs <- ifelse(...)但管道 %>%更容易和更酷;)

一旦内置,您就可以继续制作您的情节如下:
plt <- ggplot(data = ddf, aes(x = lon, y = lat, color = td)) +
geom_point(aes(shape = td, size = abs_trs, fill = td), alpha = .5) +
scale_shape_manual(values = c(25, 24)) +
scale_color_manual(values=c("blue", "red")) +
scale_size_continuous(range = c(2,20)) +
scale_fill_manual(values = c("blue","red")) +
geom_text(aes(label = round(all_trs,1))) +
guides(size = F)
plt

enter image description here

我包括了 geom_text覆盖以帮助显示大小正确缩放。希望有帮助!

关于r - 如何在 geom_point() 中反转大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com