R 在ggplot中添加符号作为标签

R 在ggplot中添加符号作为标签,r,ggplot2,maps,ggmap,R,Ggplot2,Maps,Ggmap,我有一个R脚本,它突出显示了美国地图上的某些州。我想强调两个功能,我一直有麻烦与第二个功能。我在下面附上了当前地图的样子 如果债券评级低于某个等级,则将整个州涂成红色。这是可行的 如果债券评级在过去几年中呈下降趋势,则在债券评级旁边给出一个红色向下箭头,作为该州的标签。这些已在下面的df dtrend中标记和过滤。 非常感谢您的帮助。谢谢 #cleanup statedata <- read.csv(file = "statebondratingsmelted.csv", header =

我有一个R脚本,它突出显示了美国地图上的某些州。我想强调两个功能,我一直有麻烦与第二个功能。我在下面附上了当前地图的样子

如果债券评级低于某个等级,则将整个州涂成红色。这是可行的 如果债券评级在过去几年中呈下降趋势,则在债券评级旁边给出一个红色向下箭头,作为该州的标签。这些已在下面的df dtrend中标记和过滤。 非常感谢您的帮助。谢谢

#cleanup
statedata <- read.csv(file = "statebondratingsmelted.csv", header = TRUE) 
head(statedata)
summary(statedata)

statedata <- subset(statedata, Year==2017)
statedata$Rank = as.numeric(levels(statedata$Rank))[statedata$Rank]
statedata$region <- tolower(statedata$region)
snames <- data.frame(region=tolower(state.name), long=state.center$x, lat=state.center$y)
snames <- merge(snames, statedata, by="region")
dtrend <- filter(mergestate, downwardtrendflag==1)
aaflag <- filter(mergestate, aa_flag==1)
us <- map_data("state")
mergestate <- merge(statedata, us, by="region")

library(ggplot2)
library(ggmap)

# Map Code
ggplot(color="black") +
    geom_map(data=us, map=us, aes(long, lat, map_id=region), fill="white", color = "#000000")+
    geom_map(data=dtrend, map=us, aes(fill="red", map_id=region), color ="#000000")+
    geom_map(data=aaflag, map=us, aes(fill="red", map_id=region), color ="#000000")+    
    geom_text(data=snames, aes(long, lat, label=Rating), size=3.0, color = "black")+
    coord_map() 
这是电流输出:

statedata <- structure(list(region = 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"), Year = c(2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L), Rating = structure(c(4L, 4L, 4L, 4L, 5L, 
4L, 5L, 7L, 7L, 7L, 6L, 6L, 9L, 7L, 7L, 5L, 3L, 4L, 4L, 7L, 6L, 
5L, 6L, 4L, 7L, 4L, 7L, 4L, 4L, 2L, 6L, 6L, 7L, 6L, 6L, 6L, 6L, 
5L, 4L, 6L, 7L, 7L, 7L, 7L, 6L, 7L, 6L, 5L, 4L, 6L), .Label = c("A", 
"A-", "A+", "AA", "AA-", "AA+", "AAA", "BBB", "BBB-", "N/A"), class = "factor"), 
    Rank = c(3, 3, 3, 3, 4, 3, 4, 1, 1, 1, 2, 2, 10, 1, 1, 4, 
    5, 3, 3, 1, 2, 4, 2, 3, 1, 3, 1, 3, 3, 7, 2, 2, 1, 2, 2, 
    2, 2, 4, 3, 2, 1, 1, 1, 1, 2, 1, 2, 4, 3, 2), downwardtrendflag = structure(c(1L, 
    2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
    1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 1L, 2L), .Label = c("0", "1", "FALSE"), class = "factor"), 
    aa_flag = c(0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L)), .Names = c("region", "Year", 
"Rating", "Rank", "downwardtrendflag", "aa_flag"), row.names = c(NA, 
50L), class = "data.frame")
问题的关键部分是询问如何绘制滴答声。如果对符号使用unicode,则符号的行为与ggplot中的文本类似,前面的回答如下:

您的问题有点复杂,因为您希望以不同的颜色绘制箭头。因此,该脚本为箭头的绘图添加了另一层

您的数据的列downwardtrendflag为1或0。我已经使用unicode\u2193为数据创建了一个标签。我改变了数据子集的方式,因为它有点混乱

# Assign arrow to flagged states
library(plyr)
library(ggplot2)
library(ggmap)

us <- map_data("state")

# Format the data
statedata <- subset(statedata, Year==2017)
statedata$Rank = as.numeric(levels(statedata$Rank))[statedata$Rank]
statedata$region <- tolower(statedata$region)

snames <- data.frame(region=tolower(state.name), long=state.center$x, lat=state.center$y)
snames <- merge(snames, statedata, by="region")

# Find states which have either a downward trend or aa flag
highlight <- filter(mergestate, downwardtrendflag==1 | aa_flag==1)

# Create label for snames
snames$downwardtrendflag <- mapvalues(snames$downwardtrendflag, from = c(0,1), to = c("", '\u2193'))
从视觉上看,这张地图并不完美,因为两组标签冲突。您最好将评级绘制为choropleth地图,然后将箭头覆盖在有问题的状态上,如下所示:

# First we must order the levels in the factor
snames$Rating <- factor(snames$Rating, levels = rev(c("BBB-", "A-","A+","AA-","AA","AA+" ,"AAA")))

# Map Code
ggplot(color="black") +
  geom_map(data=snames, map=us, aes(map_id=region, fill = Rating), color = "#000000") +
  geom_map(data=highlight, map=us, aes(map_id=region), fill="#F8766D", color ="#000000") +
  geom_text(data=snames, aes(long, lat, label=downwardtrendlabel), size=3.0, color = "red")+
  scale_fill_brewer(palette = "Reds") +
  coord_map(ylim = c(25, 50)) + 
  labs(title = " US States Debt Rating", subtitle = " Arrows highlights states which have a dowward trend", caption = "Source: Add source of data") +
  theme_nothing(legend = TRUE)

请包括所有代码,包括使用的所有数据集。如果数据来自外部文件,则可以使用dput轻松地将其转换为R对象。我在问题中添加了完整的代码。这将有助于获得statedata。请参阅我关于使用dput包含此内容的建议。是的!只是补充了一个问题。如果还有什么有用的,请告诉我。希望我的回答有帮助。ggplot代码有很多格式错误,特别是在使用aes命令时。我建议您多读一些:
# First we must order the levels in the factor
snames$Rating <- factor(snames$Rating, levels = rev(c("BBB-", "A-","A+","AA-","AA","AA+" ,"AAA")))

# Map Code
ggplot(color="black") +
  geom_map(data=snames, map=us, aes(map_id=region, fill = Rating), color = "#000000") +
  geom_map(data=highlight, map=us, aes(map_id=region), fill="#F8766D", color ="#000000") +
  geom_text(data=snames, aes(long, lat, label=downwardtrendlabel), size=3.0, color = "red")+
  scale_fill_brewer(palette = "Reds") +
  coord_map(ylim = c(25, 50)) + 
  labs(title = " US States Debt Rating", subtitle = " Arrows highlights states which have a dowward trend", caption = "Source: Add source of data") +
  theme_nothing(legend = TRUE)