Related
I am trying to highlight single 1x1 degree grid squares on a map.
It works highlighting individual grid squares for the first 1 squares but after highlight the 4th square it begins to highlight multiple groups of squares and I am not sure why?
library(ggOceanMaps)
#devtools::install_github("MikkoVihtakari/ggOceanMapsData")
library(ggOceanMapsData)
dt <- data.frame(lon = c(35, 35, 60, 60), lat = c(-25, -25, -40, -40))
grid_2019_1 <- data.frame(lat=c(-28, -29), long=c(51, 52))
grid_2019_2 <- data.frame(lat=c(-28, -29), long=c(52, 53))
grid_2019_3 <- data.frame(lat=c(-28, -29), long=c(53, 54))
grid_2019_4 <- data.frame(lat=c(-30, -31), long=c(41, 42))
grid_2019_5 <- data.frame(lat=c(-30, -31), long=c(42, 43))
P4 = basemap(data = dt,bathymetry = T,
lon.interval = 1,
lat.interval = 1,
bathy.style = "contour_blues",
bathy.border.col = NA,
bathy.size = 0.1,
bathy.alpha = 1)
P4.1 = P4 + stat_density2d(data = grid_2019_1, aes(x = long, y = lat, fill = ..density..),
geom = 'tile', contour = F)
P4.2 = P4.1 + stat_density2d(data = grid_2019_2, aes(x = long, y = lat, fill = ..density..),
geom = 'tile', contour = F)
P4.3 = P4.2 + stat_density2d(data = grid_2019_3, aes(x = long, y = lat, fill = ..density..),
geom = 'tile', contour = F)
P4.4 = P4.3 + stat_density2d(data = grid_2019_4, aes(x = long, y = lat, fill = ..density..),
geom = 'tile', contour = F)
P4.5 = P4.4 + stat_density2d(data = grid_2019_5, aes(x = long, y = lat, fill = ..density..),
geom = 'tile', contour = F)
Fixed using geom_tile
P4 = basemap(data = dt,bathymetry = T,
lon.interval = 1,
lat.interval = 1,
bathy.style = "contour_blues",
bathy.border.col = NA,
bathy.size = 0.1,
bathy.alpha = 1)
grid_2019_1 <- data.frame(lat=c(-28.5), long=c(51.5))
grid_2019_8 <- data.frame(lat=c(-31.5), long=c(42.5))
P4.1 = P4 + geom_tile(data = grid_2019_8, aes(x= long, y = lat, fill= 'red' ))
P4.1
P4.8 = P4 + geom_tile(data = grid_2019_8, aes(x= long, y = lat, fill= 'red' ))
P4.8
This question already has an answer here:
Circular barchart customization from r-graph-gallery
(1 answer)
Closed 8 months ago.
I am trying to run this code from this link https://www.r-graph-gallery.com/299-circular-stacked-barplot.html.
# library
library(tidyverse)
library(viridis)
# Create dataset
data <- data.frame(
individual=paste( "Mister ", seq(1,60), sep=""),
group=c( rep('A', 10), rep('B', 30), rep('C', 14), rep('D', 6)) ,
value1=sample( seq(10,100), 60, replace=T),
value2=sample( seq(10,100), 60, replace=T),
value3=sample( seq(10,100), 60, replace=T)
)
# Transform data in a tidy format (long format)
data <- data %>% gather(key = "observation", value="value", -c(1,2))
# Set a number of 'empty bar' to add at the end of each group
empty_bar <- 2
nObsType <- nlevels(as.factor(data$observation))
to_add <- data.frame( matrix(NA, empty_bar*nlevels(data$group)*nObsType, ncol(data)) )
colnames(to_add) <- colnames(data)
to_add$group <- rep(levels(data$group), each=empty_bar*nObsType )
data <- rbind(data, to_add)
data <- data %>% arrange(group, individual)
data$id <- rep( seq(1, nrow(data)/nObsType) , each=nObsType)
# Get the name and the y position of each label
label_data <- data %>% group_by(id, individual) %>% summarize(tot=sum(value))
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
# prepare a data frame for base lines
base_data <- data %>%
group_by(group) %>%
summarize(start=min(id), end=max(id) - empty_bar) %>%
rowwise() %>%
mutate(title=mean(c(start, end)))
# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]
# Make the plot
p <- ggplot(data) +
# Add the stacked bar
geom_bar(aes(x=as.factor(id), y=value, fill=observation), stat="identity", alpha=0.5) +
scale_fill_viridis(discrete=TRUE) +
# Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 50, xend = start, yend = 50), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 100, xend = start, yend = 100), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 150, xend = start, yend = 150), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 200, xend = start, yend = 200), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
# Add text showing the value of each 100/75/50/25 lines
ggplot2::annotate("text", x = rep(max(data$id),5), y = c(0, 50, 100, 150, 200), label = c("0", "50", "100", "150", "200") , color="grey", size=6 , angle=0, fontface="bold", hjust=1) +
ylim(-150,max(label_data$tot, na.rm=T)) +
theme_minimal() +
theme(
legend.position = "none",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar() +
# Add labels on top of each bar
geom_text(data=label_data, aes(x=id, y=tot+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=5, angle= label_data$angle, inherit.aes = FALSE ) +
# Add base line information
geom_segment(data=base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha=0.8, size=0.6 , inherit.aes = FALSE ) +
geom_text(data=base_data, aes(x = title, y = -18, label=group), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)
# Save at png
ggsave(p, file="output.png", width=10, height=10)
However, I am not sure why I am not getting the gaps and the scales in my figure (see below). As depicted, the numbers are printed inside the figure and the gaps between different groups of data are not there.
The original figure should be as follows:
There is a bug in the code. group has to be a factor to make the code adding the gaps work. To fix this add data$group <- factor(data$group).
Note: My guess is that the reason for this bug is that as of version 4.0.0 R treats strings in data frames as strings rather than factors. Hence, for versions < 4.0.0 the code worked fine as is.
# library
library(tidyverse)
library(viridis)
#> Loading required package: viridisLite
# Create dataset
data <- data.frame(
individual = paste("Mister ", seq(1, 60), sep = ""),
group = c(rep("A", 10), rep("B", 30), rep("C", 14), rep("D", 6)),
value1 = sample(seq(10, 100), 60, replace = T),
value2 = sample(seq(10, 100), 60, replace = T),
value3 = sample(seq(10, 100), 60, replace = T)
)
# Convert to factor
data$group <- factor(data$group)
# Transform data in a tidy format (long format)
data <- data %>% gather(key = "observation", value = "value", -c(1, 2))
# Set a number of 'empty bar' to add at the end of each group
empty_bar <- 2
nObsType <- nlevels(as.factor(data$observation))
to_add <- data.frame(matrix(NA, empty_bar * nlevels(data$group) * nObsType, ncol(data)))
colnames(to_add) <- colnames(data)
to_add$group <- rep(levels(data$group), each = empty_bar * nObsType)
data <- rbind(data, to_add)
data <- data %>% arrange(group, individual)
data$id <- rep(seq(1, nrow(data) / nObsType), each = nObsType)
# Get the name and the y position of each label
label_data <- data %>%
group_by(id, individual) %>%
summarize(tot = sum(value))
#> `summarise()` has grouped output by 'id'. You can override using the `.groups`
#> argument.
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id - 0.5) / number_of_bar # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse(angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle + 180, angle)
# prepare a data frame for base lines
base_data <- data %>%
group_by(group) %>%
summarize(start = min(id), end = max(id) - empty_bar) %>%
rowwise() %>%
mutate(title = mean(c(start, end)))
# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[c(nrow(grid_data), 1:nrow(grid_data) - 1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1, ]
# Make the plot
ggplot(data) +
# Add the stacked bar
geom_bar(aes(x = as.factor(id), y = value, fill = observation), stat = "identity", alpha = 0.5) +
scale_fill_viridis(discrete = TRUE) +
# Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
geom_segment(data = grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) +
geom_segment(data = grid_data, aes(x = end, y = 50, xend = start, yend = 50), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) +
geom_segment(data = grid_data, aes(x = end, y = 100, xend = start, yend = 100), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) +
geom_segment(data = grid_data, aes(x = end, y = 150, xend = start, yend = 150), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) +
geom_segment(data = grid_data, aes(x = end, y = 200, xend = start, yend = 200), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE) +
# Add text showing the value of each 100/75/50/25 lines
ggplot2::annotate("text", x = rep(max(data$id), 5), y = c(0, 50, 100, 150, 200), label = c("0", "50", "100", "150", "200"), color = "grey", size = 6, angle = 0, fontface = "bold", hjust = 1) +
ylim(-150, max(label_data$tot, na.rm = T)) +
theme_minimal() +
theme(
legend.position = "none",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1, 4), "cm")
) +
coord_polar() +
# Add labels on top of each bar
geom_text(data = label_data, aes(x = id, y = tot + 10, label = individual, hjust = hjust), color = "black", fontface = "bold", alpha = 0.6, size = 5, angle = label_data$angle, inherit.aes = FALSE) +
# Add base line information
geom_segment(data = base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha = 0.8, size = 0.6, inherit.aes = FALSE) +
geom_text(data = base_data, aes(x = title, y = -18, label = group), hjust = c(1, 1, 0, 0), colour = "black", alpha = 0.8, size = 4, fontface = "bold", inherit.aes = FALSE)
#> Warning: Removed 24 rows containing missing values (position_stack).
#> Warning: Removed 9 rows containing missing values (geom_text).
I am trying to draw a box on top of the plot on a specific x = Date and y = Price.
I have multiple Date entries stored in specificDates, but even though the code can be ran and doesn't output any errors, the box doesn't show on the plot.
dataDate <- as.Date(c("2015-01-01","2016-03-01","2018-06-01","2020-08-01"))
dataPrice <- c(170, 320, 7000,8000)
dummyData <- data.frame(dataDate, dataPrice)
specificDates <- as.Date(c("2016-07-15", "2020-05-20"))
plot_linPrice <- ggplot(data = dummyData,
mapping = aes(x = dataDate, y = dataPrice)) +
geom_line() +
scale_y_log10() +
geom_vline(xintercept = as.numeric(specificDates), color = "blue", lwd = .5) #+ #uncommenting + brings up error
geom_rect(aes(xmin = "2015-01-01", xmax = "2015-06-01", ymin = 5000, ymax = 8000), fill = "blue")
print(plot_linPrice)
Try with this:
library(ggplot2)
#Data
dataDate <- as.Date(c("2015-01-01","2016-03-01","2018-06-01","2020-08-01"))
dataPrice <- c(170, 320, 7000,8000)
dummyData <- data.frame(dataDate, dataPrice)
specificDates <- as.Date(c("2016-07-15", "2020-05-20"))
#Code
ggplot(data = dummyData,
mapping = aes(x = dataDate, y = dataPrice)) +
geom_line() +
scale_y_log10() +
geom_vline(xintercept = as.numeric(specificDates), color = "blue", lwd = .5)+
geom_rect(aes(xmin = as.Date("2015-01-01"), xmax = as.Date("2015-06-01"), ymin = 5000, ymax = 8000), fill = "blue")
Output:
I do not understand why ggtitle("My Title") + or labs(title = "My Title") + is not displaying my title. Here is my code:
require(raster)
country <- getData("GADM", country="Australia",level = 0)
points <- data.frame(id = c(1:5), lon = c(125, 144, 150, 115, 139), lat = c(-20, -15, -34, -25, -21))
edges <- data.frame(from.lon = c(144, 139, 125), from.lat = c(-15,-21, -20), to.lon = c(150, 125, 144), to.lat = c(-34, -20, -15), resource_id = c(1:3))
centrepoint <- as.numeric(geocode("Australia"))
p1 <- ggmap(get_googlemap(center = centrepoint, scale = 2, zoom = 4, maptype = "satellite"), extent = "device") +
geom_polygon(data = country, aes( x = long, y = lat, group = group), fill = NA, color = "white", size = 0.25) +
geom_segment(data = filter(edges, edgelist$resource_id == 2),
size = 0.5,
color = "pink",
aes(y = from.lat, x = from.lon, yend = to.lat, xend = to.lon),
arrow = arrow(length = unit(0.25, "cm"), type = "closed")) +
coord_fixed(1.3) +
geom_point(aes(x = lon, y = lat), data = points, col = "pink", alpha = 0.5, size = 1.0) +
ggtitle("Money") +
theme(plot.margin = unit(c(1,1,1,1), "cm"))
p1
I am still learning ggplot.
It is a bit difficult to answer this question without at least minimal reproducible data. Without that information, it seems as though there is a problem with one of your geom calls (my guess is geom_polygon()) or associated data since this works fine:
library(ggplot2)
library(ggmap)
ggmap(
get_googlemap(
scale = 2,
zoom = 7,
maptype = "satellite"
),
extent = "device") +
coord_fixed(1) +
labs(title = "Money") +
# ggtitle("Money)
theme(plot.margin = unit(c(1,1,1,1), "cm"))
I am trying to create a map that to show some study sites in three states. I would like to get rid of the black border lines that go through the map. Like below:
lon <- c(-89.105917,-89.377778,-86.700278,-86.677361,-87.338083,-87.340444)
lat <- c(37.358694, 37.215278,38.460528,38.448389,37.594583,37.5945)
#crop
lon1 <- c(-86.6214142,-87.3423767,-87.6656265,-87.1565475,-87.8155823,-87.3194199,-87.3565598)
lat1 <- c(38.484581,37.7038918,37.7400513,38.0794983,37.6372185,37.4466667,37.3590546)
#CRP
lon2 <-c(-88.4263,-87.4707718,-86.435585,-87.9516907,-89.2439117,-88.3630524,-89.0109711)
lat2 <- c(37.3582993,37.5196114,37.5220261,37.4958801,37.3413811,37.2275009,37.3633308)
#Forest
lon3 <-c(-86.608551,-87.3794403,-88.9937515,-86.7436066,-86.7483826)
lat3 <- c(38.2506294,36.9505539,37.4111404,38.1277695,37.1684914)
#Pasture
lon4 <-c(-86.6036377,-86.2461395,-86.9746704,-87.4977493,-88.9970474,-86.2609634,-86.6067734,-86.9820709)
lat4 <- c(37.0606689,37.8114433,37.5391922,37.8073006,37.4703789,37.3089409,38.1600189,37.6018295)
df <- as.data.frame(cbind(lon,lat))
df1 <- as.data.frame(cbind(lon1,lat1))
df2 <- as.data.frame(cbind(lon2,lat2))
df3 <- as.data.frame(cbind(lon3,lat3))
df4 <- as.data.frame(cbind(lon4,lat4))
pdf("/Users/tribaker/Desktop/Thesis/RaCA/RaCASites.pdf")
al1 = get_map(location = c("posey county,indiana"),
zoom = 8, maptype = 'satellite')
mdat <- map_data('state',Fill=TRUE)
ggmap(al1) +
geom_path(data=mdat,aes(x=long,y=lat, regions=c('"Kentucky","Illinois","Indiana"')),colour="black",alpha=1)+
borders("county", colour="grey60", alpha=.5)+
borders("state", colour="black", alpha=.8)+
geom_point(data = df, aes(x = lon, y = lat,colour = "Study Site", alpha = 0.8), size = 8, shape = 15) +
geom_point(data = df1, aes(x = lon1, y = lat1,colour = "Crop",fill=TRUE, alpha = 0.8), size = 8, shape = 16) +
geom_point(data = df2, aes(x = lon2, y = lat2,colour = "CRP", fill = TRUE ,alpha = 0.8), size = 8, shape = 16) +
geom_point(data = df3, aes(x = lon3, y = lat3, colour = "Forest",fill = TRUE,alpha = 0.8), size = 8, shape =16) +
geom_point(data = df4, aes(x = lon4, y = lat4,colour = "Pasture",fill = TRUE,alpha = 0.8), size = 8, shape = 16) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)
geom_text(aes(label = state), data = mdat, size = 2, angle = 45)
thanks in advance
I couldn't get the borders function to work correctly, but you can just do it manually...
Create an mdat2 dataframe with the county data and draw the borders yourself...
lon <- c(-89.105917,-89.377778,-86.700278,-86.677361,-87.338083,-87.340444)
lat <- c(37.358694, 37.215278,38.460528,38.448389,37.594583,37.5945)
#crop
lon1 <- c(-86.6214142,-87.3423767,-87.6656265,-87.1565475,-87.8155823,-87.3194199,-87.3565598)
lat1 <- c(38.484581,37.7038918,37.7400513,38.0794983,37.6372185,37.4466667,37.3590546)
#CRP
lon2 <-c(-88.4263,-87.4707718,-86.435585,-87.9516907,-89.2439117,-88.3630524,-89.0109711)
lat2 <- c(37.3582993,37.5196114,37.5220261,37.4958801,37.3413811,37.2275009,37.3633308)
#Forest
lon3 <-c(-86.608551,-87.3794403,-88.9937515,-86.7436066,-86.7483826)
lat3 <- c(38.2506294,36.9505539,37.4111404,38.1277695,37.1684914)
#Pasture
lon4 <-c(-86.6036377,-86.2461395,-86.9746704,-87.4977493,-88.9970474,-86.2609634,-86.6067734,-86.9820709)
lat4 <- c(37.0606689,37.8114433,37.5391922,37.8073006,37.4703789,37.3089409,38.1600189,37.6018295)
df <- as.data.frame(cbind(lon,lat))
df1 <- as.data.frame(cbind(lon1,lat1))
df2 <- as.data.frame(cbind(lon2,lat2))
df3 <- as.data.frame(cbind(lon3,lat3))
df4 <- as.data.frame(cbind(lon4,lat4))
al1 = get_map(location = c("posey county,indiana"),
zoom = 8, maptype = 'satellite')
mdat <- map_data('state', regions=c("Kentucky","Illinois","Indiana"))
mdat2 <- map_data('county', regions=c("Kentucky","Illinois","Indiana"))
ggmap(al1) +
geom_path(data=mdat2,aes(x=long,y=lat,group=group), colour="grey60", alpha=.5)+
geom_path(data=mdat,aes(x=long,y=lat,group=group), colour="black", alpha=.8)+
geom_point(data = df, aes(x = lon, y = lat,colour = "Study Site", alpha = 0.8), size = 8, shape = 15) +
geom_point(data = df1, aes(x = lon1, y = lat1,colour = "Crop",fill=TRUE, alpha = 0.8), size = 8, shape = 16) +
geom_point(data = df2, aes(x = lon2, y = lat2,colour = "CRP", fill = TRUE ,alpha = 0.8), size = 8, shape = 16) +
geom_point(data = df3, aes(x = lon3, y = lat3, colour = "Forest",fill = TRUE,alpha = 0.8), size = 8, shape =16) +
geom_point(data = df4, aes(x = lon4, y = lat4,colour = "Pasture",fill = TRUE,alpha = 0.8), size = 8, shape = 16) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)
geom_text(aes(label = state), data = mdat, size = 2, angle = 45)