我正在 R 中使用 Plotly。我想创建一个世界地图,并有两个显示不同值的图例。第一个是与每个国家发表的论文数量相对应的颜色。第二个对应于每篇论文中使用的砧木数量。随着纸张中使用的砧木越多,这些点就会变得越大。因此,我想显示一个图例,显示什么尺寸适合砧木的数量。
但是,颜色条似乎不允许我添加值。当我删除此功能时,砧木图例将起作用。我尝试了很多不同的代码,也尝试用 ggplot 来做到这一点,但没有成功。
这是我的代码,后面是我的数据集。
fig_jittered <- plot_geo(occurence) %>%
layout(
geo = list(
showframe = TRUE,
showcoastlines = TRUE,
showland = TRUE,
landcolor = toRGB("white"),
countrycolor = toRGB("darkgrey"),
coastlinecolor = toRGB("black"),
coastlinewidth = 0.5,
lataxis = list(
range = c(-55, 80),
showgrid = FALSE
),
lonaxis = list(
range = c(-130, 160),
showgrid = FALSE
)
)
) %>%
add_trace(
z = ~count, color = ~count, colors = 'GnBu',
text = ~COUNTRY, locations = ~code,
marker = list(
line = list(width = 0.5, color = "black")
),
colorbar = list(title = "Number of papers published in each country", x = 1, y = 0.92)
) %>%
add_trace(
type = "scattergeo",
lat = ~duse_no_dupplicats$lat_jittered,
lon = ~duse_no_dupplicats$lon_jittered,
text = ~paste("Title: ", duse_no_dupplicats$title, "<br>Number of Rootstocks: ", duse_no_dupplicats$n_rootstock),
mode = "markers",
marker = list(
size = duse_no_dupplicats$dotsize_rootstock,
symbol = "circle",
color = "lightgrey",
line = list(width = 1.5, color = "black")
),
name = "Number of Rootstocks" # Add the legend label
) %>%
layout(
title = "With jittered locations",
legend = list( # Customize the legend position and appearance
x = 0.05,
y = 0.95,
bgcolor = "rgba(255, 255, 255, 0.7)",
bordercolor = "black",
borderwidth = 1
)
)
fig_jittered
duse_no_dupplicats
> dput(dusecut)
structure(list(id = c("1", "5", "10", "20", "21", "24", "25",
"47", "58", "65", "68", "76", "83", "91", "103", "114", "116",
"124", "127", "134", "135", "140", "141", "142", "145", "146",
"174", "176", "180", "184", "188", "195", "208", "214", "216",
"219", "222", "233", "249", "268"), lat_jittered = c(38.7377064359404,
43.5761112164898, 37.9235031866915, 46.3098119040726, 36.5433768246047,
33.7332840405519, 43.5035202752525, 44.7743270278773, 44.2681201993407,
32.7704741151927, 34.0638900351183, 29.4918581168696, 48.3634134447124,
46.9264395307103, 44.9000974477011, 50.0297460009899, -33.9590650219929,
40.0440757318027, 47.8230118127686, 38.5980070016817, 37.1718596254961,
-23.126341973152, 39.3257486161124, -9.22739536766893, -9.06022982656411,
-20.3531972613832, 38.4468812217009, 38.7332140110433, 41.4678715980339,
-34.1984225050714, -29.2180914001753, 44.0357768001092, 47.6772796015891,
-34.3220146662689, 30.8555836620263, 42.8991637902538, 43.9426254258141,
50.0253479496291, 43.9733679219306, 38.2798062185569), lon_jittered = c(-8.32537908562589,
12.0947568336135, -121.691289428227, -119.075707938975, -118.580631370821,
73.3190522662625, 12.1431015449209, -0.146355702619443, 86.4102098408073,
51.5930024228714, -116.740621781656, 80.6078221716285, 16.6181140353805,
16.6316830731611, 0.251698475820558, 7.22708447587718, 18.0598971270631,
116.675036072787, 16.7021728935854, -120.951964736247, -94.7868793825128,
-46.3831338794722, 118.427028340939, -39.6900623886666, -39.8427074421888,
-49.7928484643629, -122.005469751341, -7.52678108194903, 15.8199758732707,
141.563458767373, -51.1325258902965, 4.46389363309673, 17.2382388971558,
142.851770006896, 33.9525991678717, -85.1173569477579, 87.8841016072193,
7.24152549942959, 4.48478443562873, -121.569625517062), n_rootstock = c(NA,
2, 15, 5, NA, 5, 2, 2, 5, 2, 3, 4, 3, 5, 3, 2, 2, 5, 1, 2, 4,
2, 7, 2, 6, 2, 8, 1, 5, 7, 15, 8, NA, 5, 2, 2, 3, 2, 1, 14),
dotsize_rootstock = c(NA, 1.5, 11.25, 3.75, NA, 3.75, 1.5,
1.5, 3.75, 1.5, 2.25, 3, 2.25, 3.75, 2.25, 1.5, 1.5, 3.75,
0.75, 1.5, 3, 1.5, 5.25, 1.5, 4.5, 1.5, 6, 0.75, 3.75, 5.25,
11.25, 6, NA, 3.75, 1.5, 1.5, 2.25, 1.5, 0.75, 10.5)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 30L, 31L,
33L, 34L, 36L, 37L, 39L, 40L, 41L, 42L, 43L, 46L, 49L, 52L), class = "data.frame")
出现
> dput(occurence)
structure(list(COUNTRY = c("Australia", "Austria", "Brazil",
"China", "Czech Republic", "Egypt", "France", "Germany", "Greece",
"Hungary", "India", "Iran", "Israel", "Italy", "Japan", "Pakistan",
"Portugal", "South Africa", "Spain", "Turquie", "United States"
), code = c("AUS", "AUT", "BRA", "CHN", "CZE", "EGY", "FRA",
"DEU", "GRC", "HUN", "IND", "IRN", "ISR", "ITA", "JPN", "PAK",
"PRT", "ZAF", "ESP", "TUR", "USA"), count = c(5L, 3L, 6L, 4L,
1L, 2L, 8L, 4L, 2L, 3L, 1L, 1L, 2L, 9L, 1L, 1L, 7L, 9L, 3L, 1L,
18L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-21L))
如果有人可以帮助我那就太好了,因为我有点迷失了。
如果您想要第二条迹线有第二个颜色条,您需要做两件事,1:您需要分配一个变量
color
和2:您需要分配colorscale
一个色标。如果您有静态颜色,则不会有色阶。
查看第二条轨迹的这种变体 - 我已经注释了我添加和注释掉的行。
顺便说一句,您调用了两次布局,我将它们合并为一次调用。
library(plotly)
plot_geo(occurence) %>%
layout(
geo = list(
showframe = TRUE, showcoastlines = TRUE, showland = TRUE,
landcolor = toRGB("white"),
countrycolor = toRGB("darkgrey"),
coastlinecolor = toRGB("black"),
coastlinewidth = 0.5,
lataxis = list(range = c(-55, 80), showgrid = FALSE),
lonaxis = list(range = c(-130, 160), showgrid = FALSE)
), title = "With jittered locations") %>%
add_trace(
z = ~count, color = ~count, colors = 'GnBu',
text = ~COUNTRY, locations = ~code,
marker = list(
line = list(width = 0.5, color = "black")
),
colorbar = list(title = "Number of papers<br />published in<br />each country",
x = 1, y = 1, len = .5) # longer to give space for title
) %>%
add_trace(
data = duse_no_dupplicats, type = "scattergeo", mode = "markers",
lat = ~lat_jittered, lon = ~lon_jittered,
text = ~paste("Title: ", duse_no_dupplicats$title, "<br>Number of Rootstocks: ",
duse_no_dupplicats$n_rootstock),
color = ~dotsize_rootstock, # <<--- I'm NEW!
marker = list(
size = ~dotsize_rootstock,
symbol = "circle",
# color = "lightgrey", # <<--- I've got to go
colorscale = 'Greys', # <<--- I'm NEW!
line = list(width = 1.5, color = "black"),
colorbar = list(title = "Root Stock", x = 1, y = .3, len = .4) # <<--- I'm NEW!
),
name = "Number of Rootstocks" # Add the legend label
)
另一个选项是添加根砧木数据图表。
在此版本中有两个图,一个是地理图,与您已经创建的类似,另一个会看起来像一个图例,但它实际上是另一个图。
plt <- plot_geo(occurence) %>%
layout(margin = list(t = 100, b = 40, r = 100, l = 40),
geo = list(
showframe = TRUE, showcoastlines = TRUE, showland = TRUE,
landcolor = toRGB("white"),
countrycolor = toRGB("darkgrey"),
coastlinecolor = toRGB("black"),
coastlinewidth = 0.5,
lataxis = list(range = c(-55, 80), showgrid = FALSE),
lonaxis = list(range = c(-130, 160), showgrid = FALSE)
), title = "With jittered locations"#,
# NEW ---- remove all legend specifics
# legend = list( # Customize the legend position and appearance
# x = 0.05, y = 0.95, bgcolor = "rgba(255, 255, 255, 0.7)",
# bordercolor = "black", borderwidth = 1)
) %>%
add_trace(
z = ~count, color = ~count, colors = 'GnBu',
text = ~COUNTRY, locations = ~code,
marker = list(
line = list(width = 0.5, color = "black")
),
colorbar = list(title = "Number of papers<br />published in<br />each country",
x = 1, y = 1, len = 1) # longer to give space for title
) %>%
add_trace(showlegend = F, # make sure to specify!
type = "scattergeo",
lat = ~duse_no_dupplicats$lat_jittered,
lon = ~duse_no_dupplicats$lon_jittered,
text = ~paste("Title: ", duse_no_dupplicats$title, "<br>Number of Rootstocks: ", duse_no_dupplicats$n_rootstock),
mode = "markers",
marker = list(
size = duse_no_dupplicats$dotsize_rootstock,
symbol = "circle",
color = "lightgrey",
line = list(width = 1.5, color = "black")
)
)
rg <- range(duse_no_dupplicats$dotsize_rootstock, na.rm = T) # range of sizes
ls <- seq(ceiling(rg[1]), ceiling(rg[2]), length.out = 5) # make an even seq
# create markers by range of sizes in root stock, showlegend = F; colorbar = NO
plt2 <- plot_ly(type = "scatter", mode = "markers", x = 1, y = ls,
showlegend = F, color = ls,
marker = list(sizeref = 0.1, sizemode = "area", size = ls,
colorscale = "Greys",
line = list(color = "black", width = 1))) %>%
layout(xaxis = list(showgrid = F, showline = F, zeroline = F, showticklabels = F),
yaxis = list(showgrid = F, zeroline = F, tickvals = ls,
ticktext = ls, tickmode = "array")) %>% hide_colorbar()
subplot(plt2, plt, widths = c(.2, .8)) %>%
layout(annotations = list( # add title to first plot (one that looks like legend)
list(xref = "x1", yref = "y1", x = 1, y = 13, showarrow = F,
text = "<br />Number of Root Stock", font = list(size = 14))
)) # show me!