I created a map of California subdivided with counties:
CAL17 <- ca_base +
theme_nothing() +
geom_polygon(data = ca_county, fill = NA, color = "yellow") +
geom_polygon(color = "black", fill = NA)
Now I am trying to add the coordinates.
Every time I try to add the coordinates the California map disappears and a single purple dot appears on an X and Y axis plot.
labs17 <- data.frame(lon17, lat17, stringsAsFactors = FALSE) +
ggplot() +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "White", size = 5) +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "yellow", size = 4)
CAL17 +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "black", size = 5) +
geom_point(data = ca_df, aes(x = lon17, y = lat17), color = "purple", size = 2)
and I get the following error
Error in FUN(X[[i]], ...) : object 'group' not found
Related
I've written this code:
ggplot() +
geom_sf(aes(fill = dat$color_province)) +
theme_void() +
geom_point(data = producer,
aes(x = producer$MX, y = producer$MY), size = 3, col = "green", shape = 17, alpha = 0.6) +
geom_point(data = distribution,
aes(x = distribution$MX, y = distribution$MY), size = 4.5, col = "yellow", shape = 15) +
geom_point(data = retailer,
aes(x = retailer$MX, y = retailer$MY), size = 3, col = "slateblue", shape = 16) +
geom_point(data = Demand,
aes(x = Demand$MX, y = Demand$MY, size = Demand$De), col = "slateblue", shape = 17, alpha = 0.7) +
scale_fill_manual(values = c("#ff3333", "#ffc266"),
name = "Situation")
and now I want to add a legend to identify all points in my plot. How can I do it?
Here's an example on some data that everyone can run, since it uses built-in datasets that come with R. Here, I made color and size be dynamic aesthetics with the name of the series, and then mapped those series values to different aesthetic values using scale_*_manual, where * are the aesthetics you want to vary by series. This generates an automatic legend. By giving each aesthetic the same name ("source" here), ggplot2 knows to combine them into one legend.
(By the way, it's unnecessary and can lead to errors to refer to variables in ggplot2 aesthetics using the form retailer$MY; each geom will assume the variable is within the data frame referred to with data =, so you can just use MY in that case.)
ggplot() +
geom_point(data = mtcars,
aes(x = wt, y = mpg, color = "mtcars", size = "mtcars")) +
geom_point(data = attitude,
aes(x = rating/20, y = complaints/3, color = "attitude", size = "attitude")) +
scale_color_manual(values = c("mtcars" = "slateblue", "attitude" = "red"), name = "source") +
scale_size_manual(values = c("mtcars" = 3, "attitude" = 4.5), name = "source")
I've got a bubble grid chart created but I can't for the life of my change the colors of the fill. I want to use a rainbow gradient based on the values. Below is my code and I've attached image out my output
setwd("C:/Users/Schelly/Desktop/Projects/Jens_tables_and_figures_2020/Bubble_chart")
library(tidyverse)
library(reshape2)
pc <- read.csv("Para_Bubble_data2.csv", header = TRUE)
head(pc)
pcm<-melt(pc, id = c("Sample"))
pcm$Sample <- factor(pcm$Sample,levels=unique(pcm$Sample))
xx = ggplot(pcm, aes(x = Sample, y = variable)) +
geom_point(aes(size = value, fill = value), alpha = 0.75, shape = 21) +
scale_colour_gradientn(colours=rainbow(4))+
scale_size_continuous(limits = c(0.000001, 1), range = c(1,17), breaks = c(.01,.10,.50,.75)) +
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")
xx
Output of code
You need to specify aes(colour = value) if you want to use scale_color_gradientn:
library(ggplot2)
df <- data.frame(x = factor(rep(1:5, each = 6)),
y = factor(rep(1:6, 5)), val = sample(30))
ggplot(df, aes(x = x, y = y, size = val, colour = val)) +
geom_point() +
scale_color_gradientn(colours = c("red", "yellow", "blue"))
If you want to use fill (to preserve a different outline colour), you need to use scale_fill_gradientn:
ggplot(df, aes(x = x, y = y, size = val)) +
geom_point(aes(size = val, fill = val), alpha = 0.75, shape = 21) +
scale_fill_gradientn(colours = rainbow(4))+
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")
In the code below, how do I remove the 2nd legend (for alpha levels, I think) and change the default blue color to, say, red?
suppressMessages(library(ggmap))
data(crime)
houston.map <- get_map(location = geocode("Houston"),
zoom = 14)
ggmap(houston.map, extent = "device", legend = "topleft") +
stat_density2d(data = crime,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 2,
bins = 10,
geom = "polygon") +
labs(fill = "Density")
Thanks.
You can use the guides layer to remove the second legend by specifying alpha as FALSE and you can change the colour scale with scale_fill_gradient as I've done below:
ggmap(houston.map, extent = "device", legend = "topleft") +
stat_density2d(data = crime,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 2,
bins = 10,
geom = "polygon") +
scale_fill_gradient(low = "#333333", high = "#cc0000") +
labs(fill = "Density") +
guides(alpha = F)
Adding guides(alpha = F) removes that legend.
suppressMessages(library(ggmap))
data(crime)
houston.map <- get_map(location = geocode("Houston"),
zoom = 14)
ggmap(houston.map, extent = "device", legend = "topleft") +
stat_density2d(data = crime,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 2,
bins = 10,
geom = "polygon") +
guides(alpha = F) +
labs(fill = "Density")
I am new in R and I want to plot a map with the distribution/ contour of pollutants and I have trouble doing that.
I a sample of my dataset is:
> head(noxco2_contour)
latitude longitude NOX.CO2
1 52.45060 -1.933200 0.01240829
2 52.45061 -1.933198 0.01045806
3 52.45060 -1.933207 0.01235869
4 52.45059 -1.933202 0.01083601
5 52.45059 -1.933199 0.01243808
6 52.45058 -1.933207 0.01379813
After getting the needed map with ggmap, I'm trying to overplot filled contours of NOX.CO2. and I am using this code:
`ggmap(west_midlands, extent = "panel") + geom_density2d(data = noxco2_contour, aes(x = longitude, y = latitude, z=NOX.CO2), size = 0.3, contour=T) +
stat_density2d(data = noxco2_contour,
aes(x = longitude, y = latitude, z= NOX.CO2, fill = ..level.., alpha = ..level..), size = 0.05,
geom = "polygon") + scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0, 1), guide = FALSE)
However I am getting the warning message of: ' Warning: Ignoring unknown aesthetics: z' and practically the fill is NOT the real values of the NOX.CO2 but a level OF unknown value/matterial. the image that i get is1 which is not the correct one.
I have checked the following relevant links Filled contour plot with R/ggplot/ggmap 3 but I get different errors and still not the correct fill.
When using:
ggmap(west_midlands, extent = "panel") + geom_density2d(data = noxco2_contour, aes(x = longitude, y = latitude), size = 0.3) +
+ stat_density2d(data = noxco2_contour,
+ aes(x = longitude, y = latitude, group= NOX.CO2, colour= NOX.CO2), size = 0.05,
+ geom = "polygon") + scale_fill_gradient(low = "green", high = "red") +
+ scale_alpha(range = c(0, 1), guide = FALSE)
I get the following: Computation failed in stat_density2d():
missing value where TRUE/FALSE needed
When I am using this code
`ggmap( west_midlands, extent = "panel" ) +
+ stat_contour(data = noxco2_contour, geom="contour", bins=20,
+ aes(x = longitude, y = latitude, z = NOX.CO2, fill = ..level.. ) ) +
+ scale_fill_continuous(name = "NOx(ppb)/CO2(ppm)", low = "yellow", high = "red" )
I am getting this Computation failed instat_contour():
Contour requires singlezat each combination ofxandy`.
I really appreciate any help
Many thanks,
Bill
I'm trying to compare two scalar fields and want to draw them in the same plot using contours labeling their values with directlabels.
The thing is, I'm not able to use two direct labels in the same plot.
Example:
library(ggplot2)
library(data.table)
library(directlabels)
grid <- expand.grid(lon = seq(0, 360, by = 2), lat = seq(-90, 0, by = 2))
grid$z <- with(grid, cos(lat*pi/180))
grid$z2 <- with(grid, sin(lat*pi/180))
grid.long <- melt(grid, id.vars = c("lon", "lat"))
# Manually adding two geom_dl's
ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "top.pieces", color = "black")
Only one variable is labeled.
Another way:
ggplot(grid.long, aes(lon, lat)) +
geom_contour(aes(z = value, color = variable)) +
geom_dl(aes(z = value, label = ..level.., color = variable),
stat = "contour", method = "top.pieces")
Any solution?
Thanks!
One solution is to provide different method= argument for the second geom_dl() call.
ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "bottom.pieces", color = "black")