ggtitle not displaying with ggmap - r

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"))

Related

gganimate for random walk model

I have created a random walk plot using ggplot2 (code below). I wondered if it would be possible to use the gganimate package so that the random walk process (the black line in the plot) gradually appears but stops once it touches the grey horizontal dashed line.
set.seed(3344)
create_random_walk <- function(number=500){
data.frame(x = rnorm(number),
rown = c(1:500)) %>%
mutate(xt = cumsum(x))
}
randomwalkdata <- rbind(mutate(create_random_walk(), run = 1))
p <- ggplot(randomwalkdata, aes(x = rown, y = xt)) +
geom_line() +
labs(x = '\nTime (arbitrary value)', y = 'Evidence accumulation\n') +
theme_classic()
p + geom_segment(aes(x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2), colour = "grey", size = 1, show.legend = FALSE) +
scale_linetype_identity()
Can anybody help?
library(gganimate); library(dplyr)
animate(
ggplot(randomwalkdata |> filter(cumsum(lag(xt, default = 0) >= 25) == 0),
aes(x = rown, y = xt)) +
geom_line() +
geom_point(data = . %>% filter(rown == max(rown)),
size = 10, shape = 21, color = "red", stroke = 2) +
labs(x = '\nTime (arbitrary value)', y = 'Evidence accumulation\n') +
theme_classic() +
annotate("segment", x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2,
colour = "grey", linewidth = 1) +
scale_linetype_identity() +
transition_reveal(rown),
end_pause = 20, width = 600)

Create a special Radial bar chart (race track plot)

I was able to replicate another good answers here to create a basic radial plot, but can anyone give me any clue of others functions/parameters/ideas on how to convert the basic one to something similar to this :
You could get pretty close like this:
df <- data.frame(x = c(10, 12.5, 15), y = c(1:3),
col = c("#fcfbfc", "#fbc3a0", "#ec6f4a"))
library(ggplot2)
ggplot(df, aes(x = 0, xend = x, y = y, yend = y, color = col)) +
geom_hline(yintercept = c(1:3), size = 14, color = "#dfdfdf") +
geom_hline(yintercept = c(1:3), size = 13, color = "#f7f7f7") +
geom_segment(color = "#bf2c23", size = 14, lineend = 'round') +
geom_segment(size = 13, lineend = 'round') +
scale_color_identity() +
geom_point(aes(x = x - 0.03 * y), size = 5, color = "#bf2c23",
shape = 21, fill = 'white') +
geom_point(aes(x = x - 0.03 * y), size = 2, color = "#bf2c23",
shape = 21, fill = 'white') +
scale_y_continuous(limits = c(0, 4)) +
scale_x_continuous(limits = c(0, 20)) +
coord_polar() +
theme_void()
Here's a start. Are there particular aspects you're trying to replicate? This is a fairly customized format.
df <- data.frame(type = c("on", "ia", "n"),
radius = c(2,3,4),
value = c(10,21,22))
library(ggplot2); library(ggforce)
ggplot(df) +
geom_link(aes(x = radius, xend = radius,
y = 0, yend = value),
size = 17, lineend = "round", color = "#bb353c") +
geom_link(aes(x = radius, xend = radius,
y = 0, yend = value, color = type),
size = 16, lineend = "round") +
geom_label(aes(radius, y = 30,
label = paste(type, ": ", value)), hjust = 1.8) +
scale_x_continuous(limits = c(0,4)) +
scale_y_continuous(limits = c(0, 30)) +
scale_color_manual(values = c("on" = "#fff7f2",
"ia" = "#f8b68f",
"n" = "#e4593a")) +
guides(color = "none") +
coord_polar(theta = "y") +
theme_void()

Interactive Maps using plotly shows lat and long as hover (needs to be datapoints) and does not work with 2 combined plots

Here is my code - first problem, I can't combine the two plots and second I am struggling to use plotly to make it interactive; My data format is also included here;
mapind <- read.csv("V2_tobacco_cancer_india2017.csv", header=T)
head(mapind)
attach(mapind)
mapbox.ind <- c(68.3,7.5,97.4,37.12)
mycol <- rgb(255, 0, 0, max = 255, alpha = 120, names = "red50")
mycol1 <- rgb(0, 0, 0, max = 255, alpha = 120, names = "blue50")
mycol2 <- rgb(0, 0, 255, max = 255, alpha = 120, names = "yellow50")
mycol3 <- rgb(34, 139, 34, max = 255, alpha = 120, names = "brown50")
inmap <- get_stamenmap(bbox=mapbox.ind, maptype="toner-lite", color = "color", zoom=6, crop=T, force=T)
p1 <- ggmap(inmap) +
geom_point(data = mapind, aes(Long, Lat), col = mycol, cex = 0.3*SLT_Prevalence, pch = 16, show.legend = T) +
geom_point(data = mapind, aes(Long, Lat), col = mycol1, cex = 0.3*Pharynx_Oral_cancer, pch = 16, show.legend = T) +
geom_label_repel(data = mapind, aes(mapind$Long, mapind$Lat), label = states, size = 2, color = "black", nudge_y = -0.6, nudge_x = -0.6, fontface = "bold", segment.color = NA) +
# geom_label_repel(data = mapind, aes(mapind$Long, mapind$Lat), label = states, size = 2, color = "black", nudge_y = 0, nudge_x = 0, fontface = "bold", segment.color = NA) +
ggtitle("B) Smokeless tobacco (red) and Oral Cancer (black)") +
theme(plot.title = element_text(size = 12)) +
theme(axis.text=element_text(size=7), axis.title=element_text(size=7,face="bold")) +
annotate("label", label = "10 Oral Cancer Cases per 100K", x = 91, y = 11.3) +
annotate("label", label = "10% SLT Prevalence", x = 91, y = 9.3)
p2 <- ggmap(inmap) +
geom_point(data = mapind, aes(Long, Lat), col = mycol2, cex = 0.3*Smoking_Prevalence, pch = 16, show.legend = T) +
geom_point(data = mapind, aes(Long, Lat), col = mycol3, cex = 0.3*Lung_cancer, pch = 16, show.legend = T) +
geom_label_repel(data = mapind, aes(mapind$Long, mapind$Lat), label = states, size = 2, color = "black", nudge_y = -0.6, nudge_x = -0.6, fontface = "bold", segment.color = NA) +
ggtitle("A) Smoking (blue) and Lung Cancer (green)") +
theme(plot.title = element_text(size = 12)) +
theme(axis.text=element_text(size=7), axis.title=element_text(size=7,face="bold")) +
annotate("label", label = "10 Lung Cancer Cases per 100K", x = 91, y = 11.3) +
annotate("label", label = "10% Smoking Prevalence", x = 91, y = 9.3)
p <- grid.arrange(p2, p1, ncol=2,
top = text_grob("Prevalence of Tobacco Use and Associated Cancer Rates in India",
size = 14),
bottom = text_grob("Source: Indian Council of Medical Research, Public Health Foundation of India, and Institute of Health Metrics and Evaluation 2017; Global Adult Tobacco Survey 2016-17",
size = 9))
p <- ggarrange(p1, p2,
labels = c("A", "B"),
ncol = 2, nrow = 1)
ggplotly(p1) %>%
highlight(
"plotly_hover",
selected = attrs_selected(line = list(color = "black"))
) %>%
widgetframe::frameWidget()

Can I plot a horizontal bar showing the distribution as as well the cutoff value?

I want to plot a bar like below.
Here is where I am.
attitude <- c('solid_blue', 'leaning_blue', 'toss_up', 'leaning_red', 'solid_red')
n_votes <- c(190, 108, 121, 39, 80)
group <- c(1,1,1,1,1)
df <- rbind(attitude, n_votes, group)
df <- as.data.frame(t(df))
ggplot(data = df) +
geom_bar(stat = 'identity', mapping = aes (x = group, y = n_votes, fill = attitude)) + coord_flip()
where df is like
attitude n_votes group
1 solid_blue 190 1
2 leaning_blue 108 1
3 toss_up 121 1
4 leaning_red 39 1
5 solid_red 80 1
I got a plot like this
The n_votes does not seem to accumulate correctly, how can I correct this?
Now I have this
Dan
Your main problem is because of the way you constructed your data frame, all the columns in it are character vectors. Look:
class(df$n_votes)
#> [1] "character"
It takes less code and gets better results to construct the data frame like this:
attitude <- c('solid_blue', 'leaning_blue', 'toss_up', 'leaning_red', 'solid_red')
n_votes <- c(190, 108, 121, 39, 80)
df <- data.frame(attitude, n_votes, group)
Now using your plot code (except noting that geom_bar(stat = "identity" is a long-hand way of writing geom_col(
ggplot(data = df) +
geom_col(aes(x = group, y = n_votes, fill = attitude)) +
coord_flip()
We get:
Or, if you want to get closer:
attitude <- c('solid_blue', 'leaning_blue', 'toss_up', 'leaning_red', 'solid_red')
n_votes <- c(190, 108, 121, 39, 80)
df <- data.frame(rev(attitude), rev(n_votes), group)
df$attitude <- factor(rev(attitude), levels = attitude)
ggplot(data = df) +
geom_col(aes(x = group, y = n_votes, fill = attitude)) +
scale_fill_manual(values = rev(c("#2558b1", "#77b3dd", "#cbcbcb", "#f3b0a7", "#d95551")),
guide = guide_none()) +
geom_text(check_overlap = TRUE, label = 298, size = 15,
x = 1.8, y = 0, hjust = 0, color = "#2558b1") +
geom_text(check_overlap = TRUE, label = 119, size = 15,
x = 1.8, y = sum(n_votes), hjust = 1, color = "#d95551") +
geom_text(check_overlap = TRUE, label = "Joe Biden\nDEMOCRAT",
size = 5, x = 1.8, y = 60, hjust = 0) +
geom_text(check_overlap = TRUE, label = "Donald J Trump\nREPUBLICAN",
size = 5, x = 1.8, y = sum(n_votes) - 60, hjust = 1) +
geom_text(check_overlap = TRUE, aes(x = group, y = n_votes, label = n_votes),
position = position_stack(vjust = 0.5), size = 5, color = "white") +
geom_segment(aes(x = 0.55, xend = 1.45, y = sum(n_votes)/2, yend = sum(n_votes)/2),
linetype = 2) +
coord_flip() +
theme_classic() +
scale_x_discrete(expand = expansion(add = c(0.7, 0.7))) +
theme(panel.background = element_rect(fill = "#fdf1e5"),
plot.margin = margin(50, 10, 50, 10),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank())

plotting rectangles and lineranges with discrete axis in ggplot2

I'm fairly new to stackoverflow.
I want to plot rectangles instead of lineranges because I want a black border. Actually my professor wants a black border but that is not an issue for stackoverflow.
Load library and create dummy dataset
library(tidyverse)
mydat <- tibble(
mymsmt = rep(c("bio", "bio", "den", "den"), 2),
mylvl = c("NT", "till", "NT", "till", "no", "yes", "no", "yes"),
mytrt = c(rep("tillage", 4), rep("herbicides", 4)),
est = c(-60, -13, -65, -40, -16, -24, -49, -50),
cilow = c(-85, -48, -78, -56, -61, -60, -68, -64),
ciup = c(8, 45, -44, -18, 79, 42, -20, -31)) %>%
# Dummy code mylvls as numeric
mutate(mylvln = rep(c(1, 2), 4))
If I plot with just the linerange, it works (I'm not allowed to embed images yet)
ggplot(mydat, aes(est, mylvl)) +
geom_linerangeh(aes(xmin = cilow, xmax = ciup), color = "blue", size = 5) +
# geom_rect(aes(xmin = cilow, xmax = ciup,
# ymin = mylvln - 0.2, ymax = mylvln + 0.2),
# fill = "red", color = "black") +
geom_point() +
facet_grid(mytrt ~ mymsmt, scales = "free")
Plot with just rectangles, fails, with
Error: Discrete value supplied to continuous scale
ggplot(mydat, aes(est, mylvl)) +
#geom_linerangeh(aes(xmin = cilow, xmax = ciup), color = "blue", size = 5) +
geom_rect(aes(xmin = cilow, xmax = ciup,
ymin = mylvln - 0.2, ymax = mylvln + 0.2),
fill = "red", color = "black") +
geom_point() +
facet_grid(mytrt ~ mymsmt, scales = "free")
Plot with linerange, covered by rectangles, works,
You can see the lineranges in the background
ggplot(mydat, aes(est, mylvl)) +
geom_linerangeh(aes(xmin = cilow, xmax = ciup), color = "blue", size = 5) +
geom_rect(aes(xmin = cilow, xmax = ciup,
ymin = mylvln - 0.2, ymax = mylvln + 0.2),
fill = "red", color = "black", alpha = 0.5) +
geom_point() +
facet_grid(mytrt ~ mymsmt, scales = "free")
Why? It works, I get the figure I want, but I don't know why. Thanks for your help!
You can also use geom_tile in place of geom_rect:
ggplot(mydat, aes(est, mylvl)) +
geom_tile(aes(width = ciup-cilow, height=0.1), fill="red", color="black") +
geom_point() +
facet_grid(mytrt ~ mymsmt, scales = "free")

Resources