是否正在向geom_point添加色彩美感?

问题描述 投票:0回答:3

我正在尝试为我的geom_point添加色彩美感,它描述了获取数据点的年份。我已经尝试过colour =fill =,但结果却不尽人意。我认为这主要与要求列名具有颜色美感有关(如果您查看df,这应该更有意义)

geom_point

这是我的代码和df来生成绘图(注意:我尝试color =“ State”知道这是错误的。 ”和“ 2015”的其他颜色):

p3 <- tidyr::pivot_longer(Unemployment_picked, -c("State", "Counties"), names_to = "Unemployment_picked", values_to = "num") %>% ggplot(aes(x=State, y=num, point = num, fill = "State")) + geom_point(stat="identity") p3 + labs(x = "State", y = "Unemployment Rate %", title = "US State Unemployment Rate") + geom_line()+ geom_text(aes(label = round(num,2)), hjust=-.2, vjust=1)
structure(list(State = structure(c(3L, 5L, 14L, 33L, 45L, 49L
), .Label = c("Alabama", "Alaska", "Arizona", "Arkansas", "California", 
              "Colorado", "Connecticut", "Delaware", "District of Columbia", 
              "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", "Puerto Rico", "Rhode Island", 
              "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", 
              "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", 
              "Wyoming"), class = "factor"), Counties = c(15L, 58L, 102L, 62L, 
                                                          254L, 39L), UE_15 = c(11.9666666666667, 10.8, 7.97450980392157, 
                                                                                7.85806451612903, 6.73818897637795, 8.84102564102564), UE_17 = c(9.76666666666667, 
                                                                                                                                                 8.26551724137931, 6.61176470588235, 6.57741935483871, 6.02834645669291, 
                                                                                                                                                 6.7025641025641)), row.names = c(3L, 5L, 14L, 33L, 45L, 49L), class = "data.frame")
r ggplot2
3个回答
0
投票
最简单的解决方案可能是pivot_

df %>% pivot_longer(UE_15:UE_17, names_to = "Year", values_to = "num") %>% mutate(Year = gsub(".*(\\d{2}$)", "20\\1", .$Year)) %>% ggplot(aes(x=State, y=num, col = Year)) + geom_point() + geom_line(aes(group = State, col = NULL))

enter image description here    

0
投票
您的绘图需要两个数据集。我称呼您所附的那个为df1,请参见下文。

library(ggplot2) df2 <- reshape(df1, idvar = "State", varying = c("UE_15", "UE_17"), direction = "long", sep = "_", timevar = "Year") ggplot() + geom_segment(data = df1, aes(x = State, xend = State, y = UE_15, yend = UE_17)) + geom_point(data = df2, aes(x = State, y = UE, color = as.factor(Year)), size = 3)

enter image description here

数据

df1 <- structure(list(State = structure(c(3L, 5L, 14L, 33L, 45L, 49L ), .Label = c("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "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", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"), class = "factor"), Counties = c(15L, 58L, 102L, 62L, 254L, 39L), UE_15 = c(11.9666666666667, 10.8, 7.97450980392157, 7.85806451612903, 6.73818897637795, 8.84102564102564), UE_17 = c(9.76666666666667, 8.26551724137931, 6.61176470588235, 6.57741935483871, 6.02834645669291, 6.7025641025641)), row.names = c(3L, 5L, 14L, 33L, 45L, 49L), class = "data.frame")

0
投票
尝试下面的代码:

library(dplyr) library(ggplot2) structure(list(State = structure(c(3L, 5L, 14L, 33L, 45L, 49L), .Label = c( "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "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", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming" ), class = "factor"), Counties = c( 15L, 58L, 102L, 62L, 254L, 39L ), UE_15 = c( 11.9666666666667, 10.8, 7.97450980392157, 7.85806451612903, 6.73818897637795, 8.84102564102564 ), UE_17 = c( 9.76666666666667, 8.26551724137931, 6.61176470588235, 6.57741935483871, 6.02834645669291, 6.7025641025641 )), row.names = c(3L, 5L, 14L, 33L, 45L, 49L), class = "data.frame") -> Unemployment_picked ggplot(Unemployment_picked, aes(x = State)) + geom_linerange(aes(ymin = UE_15, ymax = UE_17)) + geom_point(aes(y = UE_15), color = "red") + geom_text(aes(y = UE_15, label = round(UE_15, 2)), hjust=-.2, vjust=1) + geom_point(aes(y = UE_17), color = "blue") + geom_text(aes(y = UE_17, label = round(UE_17,2)), hjust=-.2, vjust=1) + labs(x = "State", y = "Unemployment Rate %", title = "US State Unemployment Rate")

© www.soinside.com 2019 - 2024. All rights reserved.