我无法使用Plotly在RStudio上渲染颜色。我正在使用R的最新版本,并且我的所有软件包都已更新。
代码如下:
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(rvest))
suppressPackageStartupMessages(library(gender))
suppressPackageStartupMessages(library(genderdata))
suppressPackageStartupMessages(library(ggrepel))
suppressPackageStartupMessages(library(janitor))
suppressPackageStartupMessages(library(maps))
suppressPackageStartupMessages(library(usmap))
suppressPackageStartupMessages(library(plotly))
# Creates variable to hold Wikipedia link
RawElectionData <- "https://en.wikipedia.org/wiki/2016_United_States_presidential_election"
# Creates variable to hold and scrape data for the 2016 election from the Wikipedia page
ElectionData <- read_html(RawElectionData) %>%
html_node(xpath = '//*[@id="mw-content-text"]/div/div[37]/table') %>%
html_table(fill=TRUE) %>% clean_names() %>% as_tibble()
# Cleaning of the data
ElectionData <- ElectionData %>%
rename(State = x,
ClintonRaw = hillary_clinton_democratic,
TrumpRaw = donald_trump_republican) %>%
select(State, ClintonRaw, TrumpRaw) %>%
slice(-c(1,10,22,23,32,33,34,58)) %>%
mutate(State=replace(State, State== "Maine (at-lg)", "Maine"),
State=replace(State, State== "Nebraska (at-lg)", "Nebraska"),
ClintonRaw=parse_number(ClintonRaw),
TrumpRaw=parse_number(TrumpRaw),
PercentTrump=round(TrumpRaw/(TrumpRaw+ClintonRaw)*100,2))
#Save data
write_csv(ElectionData, path="ElectionDataChpt2.csv")
ElectionDataWithHover <- ElectionData
ElectionDataWithHover$hover <- with(
ElectionDataWithHover,
paste(State, "\n", "Percent Trump: ", PercentTrump, "%")
)
boundaries<- list(color=toRGB("white"), width=2)
projection <- list(
scope = 'usa',
projection = list(type='albers usa'),
showlakes = TRUE,
lakecolor=toRGB('white')
)
plotattempt2 <- plot_geo(
ElectionDataWithHover,
locationmode= 'USA-states') %>%
add_trace(
z = ~PercentTrump,
text = ~hover,
locations = ~State,
color = ~PercentTrump,
colors = 'Reds') %>%
colorbar(title = "Percent of Trump Voters" ) %>%
layout(title='Percentage of Trump Voters by State (2016)', geo = projection)
plotattempt2
当我运行它时,没有任何错误,但是实际的阴谋节律上的颜色没有显示。这是输出。 The output remains the same regardless of whether I'm using the RStudio viewer or viewing the output in browser.
答案 0 :(得分:0)
您似乎需要每个州的代码。将代码(取自https://plot.ly/r/choropleth-maps/)添加到数据集中:
ElectionDataWithHover_df <- structure(list(State = c("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming"), ClintonRaw = c(729547,
116454, 1161167, 380494, 8753788, 1338870, 897572, 235603, 4504975,
1877963, 266891, 189765, 3090729, 1033126, 653669, 427005, 628854,
780154, 357735, 1677928, 1995196, 2268839, 1367716, 485131, 1071068,
177709, 284494, 539260, 348526, 2148278, 385234, 4556124, 2189316,
93758, 2394164, 420375, 1002106, 2926441, 252525, 855373, 117458,
870695, 3877868, 310676, 178573, 1981473, 1742718, 188794, 1382536,
55973), TrumpRaw = c(1318255, 163387, 1252401, 684872, 4483810,
1202484, 673215, 185127, 4617886, 2089104, 128847, 409055, 2146015,
1557286, 800983, 671018, 1202971, 1178638, 335593, 943169, 1090893,
2279543, 1322951, 700714, 1594511, 279240, 495961, 512058, 345790,
1601933, 319667, 2819534, 2362631, 216794, 2841005, 949136, 782403,
2970733, 180543, 1155389, 227721, 1522925, 4685047, 515231, 95369,
1769443, 1221747, 489371, 1405284, 174419), PercentTrump = c(64.37,
58.39, 51.89, 64.29, 33.87, 47.32, 42.86, 44, 50.62, 52.66, 32.56,
68.31, 40.98, 60.12, 55.06, 61.11, 65.67, 60.17, 48.4, 35.98,
35.35, 50.12, 49.17, 59.09, 59.82, 61.11, 63.55, 48.71, 49.8,
42.72, 45.35, 38.23, 51.9, 69.81, 54.27, 69.3, 43.84, 50.38,
41.69, 57.46, 65.97, 63.62, 54.71, 62.38, 34.81, 47.17, 41.21,
72.16, 50.41, 75.71), hover = c("Alabama \n Percent Trump: 64.37 %",
"Alaska \n Percent Trump: 58.39 %", "Arizona \n Percent Trump: 51.89 %",
"Arkansas \n Percent Trump: 64.29 %", "California \n Percent Trump: 33.87 %",
"Colorado \n Percent Trump: 47.32 %", "Connecticut \n Percent Trump: 42.86 %",
"Delaware \n Percent Trump: 44 %", "Florida \n Percent Trump: 50.62 %",
"Georgia \n Percent Trump: 52.66 %", "Hawaii \n Percent Trump: 32.56 %",
"Idaho \n Percent Trump: 68.31 %", "Illinois \n Percent Trump: 40.98 %",
"Indiana \n Percent Trump: 60.12 %", "Iowa \n Percent Trump: 55.06 %",
"Kansas \n Percent Trump: 61.11 %", "Kentucky \n Percent Trump: 65.67 %",
"Louisiana \n Percent Trump: 60.17 %", "Maine \n Percent Trump: 48.4 %",
"Maryland \n Percent Trump: 35.98 %", "Massachusetts \n Percent Trump: 35.35 %",
"Michigan \n Percent Trump: 50.12 %", "Minnesota \n Percent Trump: 49.17 %",
"Mississippi \n Percent Trump: 59.09 %", "Missouri \n Percent Trump: 59.82 %",
"Montana \n Percent Trump: 61.11 %", "Nebraska \n Percent Trump: 63.55 %",
"Nevada \n Percent Trump: 48.71 %", "New Hampshire \n Percent Trump: 49.8 %",
"New Jersey \n Percent Trump: 42.72 %", "New Mexico \n Percent Trump: 45.35 %",
"New York \n Percent Trump: 38.23 %", "North Carolina \n Percent Trump: 51.9 %",
"North Dakota \n Percent Trump: 69.81 %", "Ohio \n Percent Trump: 54.27 %",
"Oklahoma \n Percent Trump: 69.3 %", "Oregon \n Percent Trump: 43.84 %",
"Pennsylvania \n Percent Trump: 50.38 %", "Rhode Island \n Percent Trump: 41.69 %",
"South Carolina \n Percent Trump: 57.46 %", "South Dakota \n Percent Trump: 65.97 %",
"Tennessee \n Percent Trump: 63.62 %", "Texas \n Percent Trump: 54.71 %",
"Utah \n Percent Trump: 62.38 %", "Vermont \n Percent Trump: 34.81 %",
"Virginia \n Percent Trump: 47.17 %", "Washington \n Percent Trump: 41.21 %",
"West Virginia \n Percent Trump: 72.16 %", "Wisconsin \n Percent Trump: 50.41 %",
"Wyoming \n Percent Trump: 75.71 %"), code = structure(c(2L,
1L, 4L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L, 15L, 12L,
16L, 17L, 18L, 21L, 20L, 19L, 22L, 23L, 25L, 24L, 26L, 29L, 33L,
30L, 31L, 32L, 34L, 27L, 28L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 46L, 45L, 47L, 49L, 48L, 50L), .Label = c("AK",
"AL", "AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "IA",
"ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN",
"MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY",
"OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA",
"VT", "WA", "WI", "WV", "WY"), class = "factor")), row.names = c(NA,
-50L), class = "data.frame")
其次:
plotattempt2 <- plot_geo(
ElectionDataWithHover_df,
locationmode= 'USA-states') %>%
add_trace(
z = ~PercentTrump,
text = ~hover,
locations = ~code,
color = ~PercentTrump,
colors = 'Reds') %>%
colorbar(title = "Percent of Trump Voters" ) %>%
layout(title='Percentage of Trump Voters by State (2016)', geo = projection)