I have created 2 plots and combined them as seen below but i now wish to plot this with one legend and the same colour scheme for both plots, does anyone know what changes i need to make this?
code used to create plots
#plotting actual from 1998-2020
mapplot_temp = ggplot(map_preds20, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot1 = mapplot_temp + scale_fill_gradient(name = 'GDP per capita change %', low =
'red', high = 'yellow', na.value = 'grey') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#plotting predicted from 2021-2040
mapplot_temp2 = ggplot(map_preds40, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot2 = mapplot_temp2 + scale_fill_gradient(name = 'GDP per capita change % ($)', low = 'red', high = 'yellow', na.value = 'grey') +
ggtitle('Predicted GDP per capita change') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#combine plots with one legend and same colour scheme
mapplot1 + mapplot2
plot
Related
This is my script for the plot,
data = data.frame(Kingdom = c("Bacteria", "Archaea"),
Total = c(273523, 2616))
sizeRange <- c(0,30)
library(ggplot2)
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
somebody, please tell me how can I get a connecting line between my y-axis label and the plot
My plot looks like this
I want something like this
A clean alternative would be to label the points directly, and remove the y-axis if wanted. e.g.:
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
ggrepel::geom_text_repel(aes(label = Kingdom), vjust = -1,colour="black") +
geom_point(aes(size = Total),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"),
axis.text.y=element_blank(),
axis.title.y = element_blank(),
axis.ticks.y=element_blank())
you can manually add segments, but then the alpha of your points will kind of show them.
Here is a try, altought it's not perfect if the x axis expend.
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
# Added the segments here before the points.
# I tried to alpha it but I can't figure out how to limit the
# segment to the point border.
geom_segment(x = rep(-100,2), xend = rep(0,2),
y = c(1, 2), yend = c(1,2),colour="blue", alpha = 10) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() + guides(alpha = "none") + # remove alpha from legend.
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
How can I implement histogram with such complex x-axis?
First x-axis row is the week start, second - week end.
Data for tests in csv: https://gofile.io/d/FrhLZh.
What I managed to
hist_data %>%
ggplot(aes(x = week, y = count)) +
geom_col(fill = "#5B879E", width = 0.9, size = 0.7) +
labs(title = "", x = "", y = "") +
theme_bw() + theme_minimal() + theme(legend.position="none")+
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(vjust = 0.5, size = 8, family = "Inter", colour = "#ffffff"),
axis.line.x = element_blank(),
axis.title.x = element_blank(),
plot.background = element_rect(fill = "#3A464F"),
plot.margin=unit(c(0,0.25,0.5,0), "cm"))+
scale_x_discrete(expand=c(0,0), labels = format(as.Date(hist_data$week_start), "%d-%m"), position = "bottom") +
scale_y_continuous()
Thanks to teunbrand and his ggh4x package, solution:
hist_data %>%
ggplot(aes(x = week, y = count)) +
geom_col(fill = "#5B879E", width = 0.8, size = 0.7)+
labs(title = "", x = "", y = "") +
theme_bw() + theme_minimal() + theme(legend.position="none")+
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(vjust = 0.5, size = 8, lineheight = 0.8, family = "Inter", colour = "#ffffff"),
axis.line.x = element_blank(),
axis.title.x = element_blank(),
ggh4x.axis.nestline.x = element_line(size = 0.5, colour = "#5B879E", lineend = "square"),
plot.background = element_rect(fill = "#3A464F"),
plot.margin=unit(c(1,0.5,1,0.5), "cm"))+
scale_x_discrete(expand=c(0,0),
labels = paste0(format(as.Date(sort(hist_data$week_start)), "%d.%m"),
"\n", "nonsense", "\n",
format(as.Date(sort(hist_data$week_end)), "%d.%m")), position = "bottom") +
scale_y_continuous() +
guides(x = guide_axis_nested(delim = "nonsense"))
You can add multiple layers of geom_text and geom_segment. Adjust the relative y positions of these layers using a scaling factor.
plotscale <- max(hist_data$count)/50
library(ggplot2)
ggplot(data = hist_data,
aes(x = week_start + floor(week_end-week_start)/2, y = count)) +
geom_col(fill = "#5B879E", width = 4) +
geom_text(aes(y = -6 * plotscale ,
label = format(week_start, "%m-%d")),
color = "#ffffff")+
geom_segment(aes(x = week_start, xend = week_end,
y = -10 * plotscale, yend = -10 * plotscale),
color = "#5B879E", size = 1.5)+
geom_text(aes(y = -14 * plotscale,
label = format(week_end, "%m-%d")),
color = "#ffffff")+
theme_minimal() +
theme(
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
plot.background = element_rect(fill = "#3A464F"))+
scale_x_date(expand=c(0,0), date_breaks = "1 week",
labels = NULL)
Consider using ggh4x package for more complex nested x axes.
Raw Data
hist_data <- read.table(text='"","week","count","week_start","week_end"
"1","1",21.5823972708382,2021-01-04,2021-01-10
"2","2",36.122556304552,2021-01-11,2021-01-17
"3","3",34.2809483156697,2021-01-18,2021-01-24
"4","4",25.8546925450454,2021-01-25,2021-01-31
"5","5",29.0309819292706,2021-02-01,2021-02-07
"6","6",33.1503608888827,2021-02-08,2021-02-14
"7","7",27.0490347440184,2021-02-15,2021-02-21
"8","8",30.3031289757874,2021-02-22,2021-02-28
"9","50",32.2876434072602,2020-12-07,2020-12-13
"10","51",33.1939593686481,2020-12-14,2020-12-20
"11","52",26.6853246329896,2020-12-21,2020-12-27
"12","53",23.0715199391151,2020-12-28,2021-01-03', header = TRUE, sep = ",")
hist_data$week_start <- as.Date(hist_data$week_start)
hist_data$week_end <- as.Date(hist_data$week_end)
I want to place ticks on all four sides of my graph. The way suggested for this is mirror_ticks.
library(ggplot2)
library(ggplotTicks)
sp6<-ggplot(Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize)))+
geom_point( data=Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize), color=PART) )+
guides( color=FALSE)
sp8<-sp6+ labs(x=expression(paste(log(s))))+
labs(y=expression(paste(log(P(s)))) )+
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=15),
axis.title.x = element_text(size=15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=2)
)
sp10<-mirror_ticks(sp10, allPanels=TRUE)
My output sp10 has no ticks on opposite panels, same result if I put allPanels=TRUE
Is there a fix? I am open to learn how one does this with theme settings?
As of ggplot2 version 2.2.0 (2016-11-11), the scale_x_continuous() and scale_y_continuous() can display a secondary axis which is positioned opposite to the primary axis and which can be controlled with the sec.axis argument.
This can be used to mirror the tick marks.
The OP hasn't provided reproducible data so we use the mpg dataset which comes with the ggplot2 package:
Chart without mirrored tick marks
library(ggplot2)
g1 <- ggplot(mpg, aes(log10(displ), log10(hwy))) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
)
g1
Chart with secondary axes
g1 +
scale_x_continuous(sec.axis = dup_axis()) +
scale_y_continuous(sec.axis = dup_axis())
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL))
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))
Mirrored tick marks with log10 scales
The secondary axes are also available with the scale_x_log10() and scale_x_log10() functions.
So, it can be avoided to use the log() function within the call to aes() but by specifying an appropriate log scale:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
) +
scale_x_log10(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_log10(sec.axis = dup_axis(name = NULL, labels = NULL))
I've had trouble increasing the radius of the circle on a map. I've tried the scale_size_continuous with maximum size up to 1000, but the circle size remained the same after all.
My code is in the following:
states <- map_data("state")
gg1 <- ggplot(data=states) +
geom_polygon(data=states, aes(x=long,y=lat,group=group),
fill = "orange", color = "white") +
coord_fixed(ratio = 1.3)+
geom_circle(data = origCity, aes(x0 = Long, y0 = Lat, r = Percent),
fill = "black", color = "black", alpha = 0.5, show.legend = FALSE) +
scale_size_continuous(range = c(20, 5))+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())+
theme(axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank())+
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank())
print(gg1)
Any suggestions are greatly appreciated!
I'm using ggplot so I can get a gradient onto a map to show data over a large scale. There are points between 0 and 35,000 to be visualised. I have got this to work, but the legend is automatically showing labels for every 10,000.
Ideally I want the legend to show the maximum amount, so probably it would just show 0 at the bottom and 35,000 at the top. Is this doable?
My ggplot code is below if this helps.
ggplot() +
geom_map(data = datafile, aes(map_id = Health_Board, fill = datafile$"2007"), map = Scot) +
geom_polygon(data = Scot, aes(x = long, y = lat, group = group), colour = "gray", fill = NA) +
expand_limits(x = Scot$long, y = Scot$lat) +
scale_fill_gradient(low = ("lightyellow"), high = ("red"), limits = c(0,35000)) +
ggtitle("2007") +
coord_fixed(1.2) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_blank(), panel.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(face = "bold", hjust = 0.5))
You can include the "breaks" argument. Like this:
scale_fill_gradient(low = ("lightyellow"), high = ("red"),
breaks=c(min(lat),max(lat)),
limits = c(0,35000)) +
If you want more, its possible to include the "labels" argument.
scale_fill_gradient(low = ("lightyellow"), high = ("red"),
breaks=c(min(lat),max(lat)),
labels=c("Minimum","Maximum"),
limits = c(0,35000)) +