How do I change the NA color from gray to white in a ggplot choropleth map?
You can change color of NA values (states without data) by changing argument na.value
in scale_fill_continuos()
.
+scale_fill_continuous(low="thistle2", high="darkred",
guide="colorbar",na.value="white")
Change range of fill values in choropleth map R
I just created a new column in the population dataframe that summarizes the population based on the ranges that I want to use, and then use that as the criteria for the fill:
txczpop$poprange[txczpop$pop2014 >= 0 & txczpop < 1000] <- "0-1,000"
txczpop$poprange[txczpop$pop2014 >= 1000 & txczpop < 10000] <- "1-10,000"
txczpop$poprange[txczpop$pop2014 >= 10000 & txczpop$pop2014 < 100000] <- "10,000-100,000"
txczpop$poprange[txczpop$pop2014 >= 100000 & txczpop$pop2014 < 1000000] <- "100,000 - 1,000,000"
txczpop$poprange[txczpop$pop2014 >= 1000000 & txczpop$pop2014 <= 5000000] <- "1,000,000 - 5,000,000"
Shading every other vertical month, alternating white and gray
I would use geom_rect
with a separate data.frame (here: shades
)
shades <- data.frame(xmin=seq(1.5,length(unique(dataset$Date))-1.5, 2),
xmax=seq(2.5,length(unique(dataset$Date))+.5, 2),
ymin=0, ymax=Inf)
ggplot(dataset,aes(x=Date,y=values,fill=type))+
geom_boxplot(position=position_dodge(width = 0.7))+
stat_boxplot(geom="errorbar",width=0.7)+
coord_cartesian(ylim = c(min.box,max.box))+
#geom_polygon(data=poly.data,mapping = aes(x=x,y=y),fill="grey30")+
#scale_y_continuous(sec.axis = sec_axis(~(.-min.box)*max.count/box.25, name = "Sec axis"),breaks = scales::pretty_breaks(n = 10))+
labs(title="Boxplot of values Over Time",y="values",x="Date (year-month)")+
theme_classic(base_size=15)+
theme(axis.text.x = element_text(angle = ifelse(noB>15,45,0), hjust=ifelse(noB>15,1,0.5)),panel.grid.major=element_line("light grey")) +
geom_rect(inherit.aes = F, data = shades, mapping = aes(xmin=xmin, xmax=xmax, ymin = ymin, ymax = ymax), alpha = 0.2)
How do I conditionally color in a US States map using ggplot?
I think this is what you want to achieve? In geom_sf
color refers to the color of the border and fill
to the color filing the polygons (for polygons).
library(ggplot2)
library(tidyverse)
library(mapdata)
#> Loading required package: maps
#>
#> Attaching package: 'maps'
#> The following object is masked from 'package:purrr':
#>
#> map
library(dplyr)
MainStates <- map_data("state")
State_Name <- c("alabama","arkansas","arizona","california","colorado")
Sales <- c(100,200,250,275,310)
df2 <- data.frame(State_Name,Sales)
MergedStates <- inner_join(MainStates, df2, by = c("region" = "State_Name"))
p <- ggplot() + geom_polygon(data = MergedStates,
aes(x=long, y=lat, group=group),
fill = ifelse(MergedStates$Sales <=
mean(MergedStates$Sales),"red","green"), size = 0.2)
p
Created on 2021-10-01 by the reprex package (v2.0.1)
Change theme ggChoropleth
With interactive=TRUE
the output of ggChoropleth
is a ggiraph
htmlwidget
object and not a ggplot
object. Hence, you cannot use + theme_bw()
.
I suggest to use the following solution:
p <- ggChoropleth(crimes, aes(fill=Murder, map_id=state),
map=states_map, interactive=F)
p <- p + theme_void()
tooltip_css <- "background-color:white;font-style:italic;padding:10px;border-radius:20px 20px 20px 20px;"
ggiraph(ggobj = p, tooltip_extra_css = tooltip_css, zoom_max = 10)
Related Topics
Using Dplyr to Conditionally Replace Values in a Column
R: Using Rgl to Generate 3D Rotatable Plots That Can Be Viewed in a Web Browser
Summing Across Rows of a Data.Table for Specific Columns
Extract Non Null Elements from a List in R
Adding Total/Subtotal to the Bottom of a Datatable in Shiny
R Dpylr Select_If with Multiple Conditions
Filter Each Column of a Data.Frame Based on a Specific Value
Convert Column in Data.Frame to Date
What Is the Correct Way to Ask for User Input in an R Program
Combination Boxplot and Histogram Using Ggplot2
How to Remove Multiple Columns in R Dataframe
Update Graph/Plot with Fixed Interval of Time
How to Use Earlier Declared Variables Within Aes in Ggplot with Special Operators (..Count.., etc.)