can not mapping discrete and continuous value to a map in r - r

As the title says.
When I mapping a continuous value to a map it works well.
When I mapping a discrete value to a map it works also well.
But when I combine the two layers an error occur:
Error: Discrete value supplied to continuous scale
Reproducible code was here:
library(scatterpie)
library(tidyverse)
library(geosphere)
us <- map_data('state') %>% as_tibble()
n = length(unique(us$region))
# creat fake mapping data
temperature_data <- tibble(region = unique(us$region),
temp = rnorm(n = n))
coords <- us %>% select(long, lat, region) %>% distinct(region, .keep_all = T)
category_data <- tibble(region = unique(us$region),
cat_1 = sample(1:100, size = n),
cat_2 = sample(1:100, size = n),
cat_3 = sample(1:100, size = n)) %>% left_join(coords)
us <- left_join(us, temperature_data)
p <- ggplot(us, aes(long, lat))
# mapping temperautre value to map
p + geom_map(map = us, aes(map_id = region, fill = temp), color = 'grey')
# mapping pie chart to map
p +
geom_map(map = us, aes(map_id = region), color = 'grey') +
geom_scatterpie(data = category_data,
aes(long, lat),
cols = c("cat_1", "cat_2", "cat_3"),
alpha = 0.5)
# mapping temperautre and pie chart simultaneously
# ERROR OCCUR
# Error: Discrete value supplied to continuous scale
p + geom_map(map = us, aes(map_id = region, fill = temp), color = 'grey') +
geom_scatterpie(data = category_data,
aes(long, lat),
cols = c("cat_1", "cat_2", "cat_3"),
alpha = 0.5)

Related

Overlaying the same line plot in every facet in ggplot2

I need to overlay the same 'target_gene' plot on all facets. I can overlay it easily if I plot everything in 1 panel. However, when I combine this action with the facet_wrap, the 'target_gene' is plotted in a separate facet, rather than being overlaid on other plots. Please see the script below. The 'select_genes_plot' works (see same panel plots) but the 'select_genes_plot_facet' doesn't work (see plot in facets).
Please not that I cannot use +annotate(geom='point',x=target_gene$gene_exp.Time,y=target_gene$gene_exp.Value) because I also want to plot the sd error bars.
The transcriptome_reshape and target_gene data look like this, please see transcriptome data example, and target gene data)
library(ggplot2)
library(dplyr)
library(reshape2)
##1. Read transcriptome dataset
setwd('./')
transcriptome_data <- read.csv ('./Kaladp_expression_FPKM_average_sd.csv', header = TRUE)
reshape_data <- melt(transcriptome_data) #the function melt reshapes it from wide to long
average_geneexpression <- reshape_data[1:(743136/2),]
sd_geneexpression <- reshape_data[((743136/2)+1):743136,]
transcriptome_reshape <- cbind(average_geneexpression, sd_geneexpression)
colnames(transcriptome_reshape)[2]<- 'gene_exp.Time'
colnames(transcriptome_reshape)[3]<- 'gene_exp.Value'
colnames(transcriptome_reshape)[4]<- 'transcript_sd_data'
colnames(transcriptome_reshape)[5]<- 'sd.Time'
colnames(transcriptome_reshape)[6]<- 'sd.Value'
##2. Select ONE key target gene
target_gene <- transcriptome_reshape %>%
filter (Transcript %in% c('Kaladp0977s0008.1'))
##3.Plot options
##plot whole genome
ggplot(data = transcriptome_reshape, aes(x = gene_exp.Time, y = gene_exp.Value, group = Transcript)) +
geom_line()
##plot target gene alone
ggplot(target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group = 1)) +
geom_line()+
geom_point()
##plot all genes on same panel
select_genes_plot <- transcriptome_reshape %>%
filter (Transcript %in% c('Kaladp0498s0001.1','Kaladp0011s0858.1','Kaladp0071s0450.1')) %>%
ggplot(mapping = aes(x = gene_exp.Time, y = gene_exp.Value, color = Transcript)) +
geom_line(aes(group=Transcript))+
geom_point()+
geom_errorbar(aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value), width = 0.2, position = position_dodge(0.0))+
geom_point(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value), color = 'black', shape = 17)+ #this line plots target gene
geom_line(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group=Transcript), color = 'black')+ #this line plots target gene
geom_errorbar(data = target_gene, aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value),color = 'black', width = 0.2, position = position_dodge(0.0))#this line plots target gene
select_genes_plot
##plot in facets
select_genes_plot_facets <- transcriptome_reshape %>%
filter (Transcript %in% c('Kaladp0498s0001.1','Kaladp0011s0858.1','Kaladp0071s0450.1')) %>%
ggplot(mapping = aes(x = gene_exp.Time, y = gene_exp.Value, color = Transcript)) +
geom_line(aes(group=Transcript))+
geom_point()+
geom_errorbar(aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value), width = 0.2, position = position_dodge(0.0))+
facet_wrap(facets = vars(Transcript), ncol = 3)+
geom_point(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value), color = 'black', shape = 17)+ #this line plots target gene
geom_line(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group = 1), color = 'black')+ #this line plots target gene
select_genes_plot_facets

Plotted points in Map doesn't vary based on the price

I plotted some data points on a map. The Map is the bounty King in Washington. I plotted them on the map successfully. My dataset consists of a column called prices. Their values range in between 0-800000.
I created a new column, called MapColor to print the value either as red or blue based on the price value. If price> 400000 red, else blue.
Now when plotting the points in the map, if MapColor is red I need to map it as red points and if not black. This is how I tried it. But the colors are plotting black only. This is what I tried
long <- c(47.5112,47.7210 ,47.3684)
lat <- c(-122.257, -122.319, -122.031)
price <- c(287655,456355,662500,234563)
House <- data.frame(long, lat, price)
House$MapColor <- ifelse(House$price >=400000, "red", "black")
col <- as.character(House$MapColor)
states <- map_data("state")
wa_df <- states %>%
filter(region == "washington", subregion == 'king')
counties <- map_data("county")
wa_county <- counties %>%
filter(region == "washington")
wa_base <-
ggplot(data = wa_df,
mapping = aes(x = long, y = lat, group = group)) + geom_point(data = House,aes(x = long, y = lat),size = 0.5,inherit.aes = FALSE) +
coord_fixed(1.3) +scale_color_manual(values=col)+
geom_polygon(color = "black", fill = "gray")
#geom_point(data = House, mapping = aes(x = long, y = lat), color = "red")
wa_base + theme_nothing() +
geom_polygon(data = wa_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA) # get the state border back on top
Please let me know if this is what you had in mind.
I think long and lat for those example points were reversed. I set the colors as either red or blue as per the description.
It maps the state polygon, then county, then adds the points using color = House$MapColor.
library(ggplot2)
library(ggmap)
lat <- c(47.5112, 47.7210, 47.3684)
long <- c(-122.257, -122.319, -122.031)
price <- c(287655, 456355, 662500)
House <- data.frame(long, lat, price)
House$MapColor <- ifelse(House$price >= 400000, "red", "blue")
wa_df <- map_data("state") %>%
filter(region == "washington")
wa_county <- map_data("county") %>%
filter(region == "washington")
ggplot(data = wa_df, mapping = aes(x = long, y = lat, group = group))+
geom_polygon(color = "black", fill = NA)+
geom_polygon(data = wa_county, fill = NA, color = "black")+
geom_point(data = House, mapping = aes(x = long, y = lat), color = House$MapColor)+
coord_fixed(1.3)+
theme_nothing()

Created labels for geom_point

library(ggplot2)
usa <- map_data("state")
myData <- data.frame(
states = c("AL","AZ","AR","CA","CO","CT","DE","FL","GA","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"),
lat = c(32,33,34,36,39,41,39,38,27,33,44,40,39,42,38,37,31,44,39,42,43,45,32,38,46,41,38,43,40,34,42,35,47,40,35,44,40,41,33,44,35,31,40,44,37,47,38,44),
long = c(-86,-111,-92,-119,-105,-72,-75,-77,-81,-83,-114,-88,-86,-93,-96,-84,-91,-69,-76,-71,-84,-93,-89,-92,-110,-98,-117,-71,-74,-106,-74,-79,-99,-82,-96,-122,-77,-71,-80,-99,-86,-97,-111,-72,-78,-121,-89,-89),
pop = c(4.8,7,3,39.5,5.6,3.5,.961,20.9,10.4,1.7,12.8,6.7,3.1,2.9,4.4,4.6,1.3,6,6.8,9.9,5.5,2.9,6.1,1,1.9,2.9,1.3,9,2,19.8,10.2,.755,11.6,3.9,4.1,12.8,1,5,.869,6,28.3,3.1,.623,8.4,7.4,1.8,1,2))
ggplot() +
geom_path(data = usa, aes(x = long, y = lat, group = group)) +
geom_point(data = myData, aes(x = long, y = lat, size = pop), color = "blue")
How would I go about adding labels to the map being created?
I assume that by "How would I go about adding labels to the map being created?" you mean how to add state labels.
There seems to be something wrong with the data in myData. The names of the states and their lat/long don't seem to match.
That aside, in general you can add labels in the following way:
ggplot() +
geom_path(data = usa, aes(x = long, y = lat, group = group)) +
geom_point(data = myData, aes(x = long, y = lat, size = pop), color = "blue") +
geom_text(data = myData, aes(long, lat, label = states))

Remove Antarctica from gglpot2 map

I'm trying to reproduce this tutorial on how to plot a scatterplot-like map. Below is the full code and the output:
library(readr)
library(dplyr)
library(DT)
datatable(rladies, rownames = FALSE,
options = list(pageLength = 5))
url_csv <- 'https://raw.githubusercontent.com/d4tagirl/R-Ladies-growth-maps/master/rladies.csv'
rladies <- read_csv(url(url_csv)) %>%
select(-1)
library(ggplot2)
library(maps)
library(ggthemes)
world <- ggplot() +
borders("world", colour = "gray85", fill = "gray80") +
theme_map()
map <- world +
geom_point(aes(x = lon, y = lat, size = followers),
data = rladies,
colour = 'purple', alpha = .5) +
scale_size_continuous(range = c(1, 8),
breaks = c(250, 500, 750, 1000)) +
labs(size = 'Followers')
I want to remove Antartica from the map so that it doesn't take so much empty space. I tried to follow the solution from another similar Stackoverflow question as follows:
world <- map_data("world") %>%
filter(region != "Antarctica") %>%
ggplot(aes(long, lat, group = paste(region, group))) +
geom_polygon() +
coord_fixed()
map <- world +
geom_point(aes(x = lon, y = lat, size = followers),
data = rladies,
colour = 'purple', alpha = .5) +
scale_size_continuous(range = c(1, 8),
breaks = c(250, 500, 750, 1000)) +
labs(size = 'Followers')
But when I try to display the map I get the following error:
Error in paste(region, group) : object 'region' not found
Is there any other way to remove Antartica?
UPDATE: Failed subset attempt
countries <- map_data("world")
map_df <- subset(countries, region != "Antarctica")
map_base <- ggplot(data = map_df, mapping = aes(x = long, y = lat, group = group)) + coord_fixed(1.3) + geom_polygon(color = "black", fill = "gray")
# The base map is created successfully but I cannot plot points on it
map_base + geom_point(aes(x = lon, y = lat, size = followers), data = rladies, colour = 'purple', alpha = .5)
Error:
Error in eval(expr, envir, enclos) : object 'group' not found
Following on hrbmstr's advice, here is a solution using a proper projection and the sf package (with geom_sf from the development version of ggplot2). Note we use coord_sf to set the limits.
library(sf)
world <- map_data("world")
world.sf <- sf::st_as_sf(world, coords = c("long", "lat"), crs = 4326) %>%
group_by(group) %>%
summarize(do_union = FALSE) %>%
st_cast("POLYGON") %>%
ungroup()
world <- ggplot() +
geom_sf(data = world.sf, colour = "gray85", fill = "gray80") +
coord_sf(ylim = c(-50, 90), datum = NA) +
theme(panel.background = element_rect(fill = 'white'))
world +
geom_point(aes(x = lon, y = lat, size = followers),
data = rladies,
colour = 'purple', alpha = .5) +
scale_size_continuous(range = c(1, 8),
breaks = c(250, 500, 750, 1000)) +
labs(size = 'Followers', x = NULL, y = NULL)
We can also use coord_cartesian(ylim = c(-50, 90)) to set the y limits.
library(ggplot2)
library(maps)
library(ggthemes)
world <- ggplot() +
borders("world", colour = "gray85", fill = "gray80") +
theme_map() +
coord_cartesian(ylim = c(-50, 90))
map <- world +
geom_point(aes(x = lon, y = lat, size = followers),
data = rladies,
colour = 'purple', alpha = .5) +
scale_size_continuous(range = c(1, 8),
breaks = c(250, 500, 750, 1000)) +
labs(size = 'Followers')
map

How to deal with a global fixed scale_fill_gradient2 for multiple plot in loop

I have a data.frame (CAMANOC) of species and Latitude/Longitude.
I applied a function and a for-loop to krige my data and usegrid.arrange to plot all plot in one window.
Unfortunately, when I use scale_fill_gradient2 the color gradient appears to have min limit and max limit from all my values. When scale_fill_gradient2 is masked, everythings work.
Here is my code:
CAMANOCKrigeage <- function(CAMANOC,CAMANOC.grid2){
plotCAMANOC <- list() #Defining a list to save my maps
for (j in 1:8) #Calling species
{
var.exp <- variogram((CAMANOC[[j]])~1, CAMANOC)
var.mod=vgm(psill = 1, model = c("Exp", "Mat", "Sph"), nugget = 100, range = 2000,alpha = c(0, 45, 90, 135))
var.fit=fit.variogram(var.exp,var.mod)
krigeage = krige(formula = (CAMANOC[[j]])~1,CAMANOC,CAMANOC.grid2,model = var.fit)
krig.output=as.data.frame(krigeage)
names(krig.output)[1:4]<-c("long","lat","var1.pred","var1.var")
layer1 <- c(geom_tile(data=krig.output,aes(fill=var1.pred)))
plotCAMANOC[[j]] <- ggplot(data = krig.output,aes(x = long, y = lat)) +
geom_polygon(data = oceanfort, aes(x = long, y = lat, group = group), fill = "white") +
geom_polygon(data = coastlinefort, aes(x = long, y = lat, group = group), fill = "grey") +
geom_path(data = landfort, aes(x = long, y = lat, group=group)) +
ggtitle(names(CAMANOC)[j]) +
coord_cartesian(xlim = c(-6,1),ylim = c(48,51)) +
scale_fill_gradient2(name=bquote(atop("", ~cell.cm^-3)),
high="green", mid="blue", low="red",
space="Lab", midpoint = median(krig.output$var1.pred)) +
labs(x = "Longitude") +
labs(y = "Latitude") +
theme_bw() +
layer1
}
return(plotCAMANOC)
}
plotCAMANOC <- CAMANOCKrigeage(CAMANOC,CAMANOC.grid2)
do.call(grid.arrange,plotCAMANOC)
For a better view, two maps I have:
I am sorry for the lack of reproductible example, I am not familiar with that.

Resources