I have written the following code to show four plots
Scores <- as.factor(sampleXYPCA$PC1)
p1 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC1)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank(),
legend.position="right", legend.direction="vertical")
Scores <- as.factor(sampleXYPCA$PC2)
p2 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC2)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
Scores <- as.factor(sampleXYPCA$PC3)
p3 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC3)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
Scores <- as.factor(sampleXYPCA$PC4)
p4 <- ggplot(sampleXYPCA, aes(x = X_UTM_, y = Y_UTM_, color=PC4)) +
geom_point( ) + scale_color_gradient(low="blue", high="red") +
geom_polygon(data = xy, aes(x = xBounds, y = yBounds),
color="orange", alpha = 0.2, show.legend = FALSE) + labs( x ="x (m) ", y = "y (m)") +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
figure <- ggarrange(p1, p2,p3,p4 + font("x.text", size = 10),
ncol = 2, nrow = 2)
show(figure)
I have two issues that I am trying to fix:
I want to remove the values at x-axis at the last plot (PC4), as in the previous plots.
I want to set the same scale at the colour bar for all plots (from -3,3)
For convenience, I copy the first lines of the dataframe (sampleXYPCA) that I am using:
X_UTM_ Y_UTM_ PC1 PC2 PC3 PC4
1 6501395 1885718 -1.37289727 2.320717816 0.93434761 1.24571643
2 6500888 1885073 -1.22111900 4.021127182 1.89434320 1.26801802
3 6500939 1885241 -0.58212873 3.301443355 -1.79458946 0.63329006
4 6500965 1884644 -1.13872381 4.521231473 2.43925215 0.53962882
5 6501608 1884654 -0.24075643 5.871225725 0.69257238 0.89294843
6 6501407 1883939 -0.15938861 3.965081981 1.40970861 -0.77825417
7 6501581 1883630 -0.59187192 2.904278269 0.40655574 -1.66513966
Using facet_wrap and adding an aerial basemap for visualisation (personal prefence when plotting spatial data):
#sample data as dput
dt <- structure(list(x = c(6501395, 6500888, 6500939, 6500965, 6501608,
6501407, 6501581), y = c(1885718, 1885073, 1885241, 1884644,
1884654, 1883939, 1883630), pca1 = c(-1.37289727, -1.221119,
-0.58212873, -1.13872381, -0.24075643, -0.15938861, -0.59187192
), pca2 = c(2.320717816, 4.021127182, 3.301443355, 4.521231473,
5.871225725, 3.965081981, 2.904278269), pca3 = c(0.93434761,
1.8943432, -1.79458946, 2.43925215, 0.69257238, 1.40970861, 0.40655574
), pca4 = c(1.24571643, 1.26801802, 0.63329006, 0.53962882, 0.89294843,
-0.77825417, -1.66513966)), class = "data.frame", row.names = c(NA,
-7L))
#load libraries
library(sf)
library(tidyr)
library(ggplot2)
library(ggspatial)
library(tmaptools)
#pivot_longer on PCA
dt <- pivot_longer(dt, cols = c("pca1", "pca2", "pca3", "pca4"), names_to = "PCA", values_to = "Score")
#convert to sf object (assumed that you use espg:32629, change to whatever you use as the coordinate system)
dt <- st_as_sf(dt, coords = c("x", "y"), crs = st_crs(32629))
#load a basemap
basemap <- read_osm(dt, type = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}", zoom = 15, ext = 1.2)
#plot
ggplot() + layer_spatial(basemap) + geom_sf(data = dt, aes(col = Score), size = 3) + facet_wrap(~PCA) + labs( x ="x (m) ", y = "y (m)") +
theme_bw() + theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank()) +
scale_x_continuous(expand = c(0.01,0.01)) +
scale_y_continuous(expand = c(0.01,0.01)) +
scale_color_gradient(low="blue", high="red")
Using some dummy data to illustrate a possible solution, this may help.
The issue in the OP's question seems to be with the call to ggarrange. Check out the documentation with ?ggarrange
library(ggpubr)
library(ggplot2)
p1 <-
ggplot(mtcars, aes(mpg, wt))+
geom_point() +
theme(axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks.x=element_blank(),axis.ticks.y=element_blank())
figure <-
ggarrange(p1, p1, p1, p1,
font.label = list(size = 10),
ncol = 2,
nrow = 2)
show(figure)
Created on 2021-12-10 by the reprex package (v2.0.1)
Related
I'm looking for some help creating a "Canopy Height Model (CHM)-Zmax-curve/line" on my las-plots. I extracted a transect with the lidR package
las_tr <- clip_transect(las, p1, p2, width = 3, xz=T)
And I plotted it with ggplot
ggplot(las_tr#data, aes(X,Z, color = Z)) +
geom_point(size = 0.5) +
coord_equal() +
theme_minimal() +
scale_color_gradientn(colours = height.colors(50)) +
xlim(-80,0) + ylim(0,45) +
labs(title = "2D profile", subtitle= paste(round(x[i]),round(y[i])))
I have this
But I would like something like this
I hope someone can help me.
library(lidR)
library(dplyr)
library(ggplot2)
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
las = readLAS(LASfile, select = "xyz")
p1 <- c(684800, y = 5017800)
p2 <- c(684900, y = 5017900)
tr <- clip_transect(las, p1, p2, width = 4, xz = T)
DSM <- tr#data[ , .(Z = max(Z)), by = list(X = plyr::round_any(X, 1))]
ggplot(tr#data) +
aes(X,Z, color = Z) +
geom_point(size = 0.5) +
geom_line(data = DSM, color = "black") +
coord_equal() +
theme_minimal() +
scale_color_gradientn(colours = height.colors(50))
Created on 2021-05-27 by the reprex package (v2.0.0)
I have two plots I just want to know how I can add a legend for the blue and gray bar charts and also could you please show me how you could also edit the legend tittle.
X1 <- c(seq(7.912087912,44.83516484,1.538461538))
X2 <- c(seq(7.912087912,49.45054945,1.538461538))
dat2 <- data.frame(x = X2 , y = rnorm(28, 26, 5))
dat1 <- data.frame(x = X1 , y = rnorm(100, 25, 4))
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y), stat = "identity",alpha = 0.3, position = "stack" ) + labs( x = " Time [ S ]", y = "Frequency") + theme_minimal() +
ggtitle("Histogram Of Time In Tank") + theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y ), stat = "identity", alpha = .3, position = "stack", fill='lightblue' , color='lightblue4')
+ scale_linetype_discrete(name = LegendTitle)
If you want a legend in ggplot, you need to have an aesthetic mapping inside your aes() or no legend will appear. Here's how we can set a mapping and then use the scale to set the colors we want
ggplot(NULL) +
geom_bar(dat1, mapping = aes(x = x, y = y, fill="Grey Bars"), stat = "identity",alpha = 0.3, position = "stack" ) +
labs( x = " Time [ S ]", y = "Frequency") +
theme_minimal() +
ggtitle("Histogram Of Time In Tank") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_bar(dat2, mapping = aes(x = x, y = y, fill='Blue Bars') , stat = "identity", alpha = .3, position = "stack", color='lightblue4') +
scale_fill_manual(name="Bars", values=c("Grey Bars" = "grey35", "Blue Bars" = "lightblue"))
I am trying to add the labels A, B, and C to the top left hand corner of each of these graphs. I have tried cowplot::draw_plot_label(), but nothing seems to work. Can anyone help?
These A, B and C labels are not the main title of each plot.
# Packages
library(ggplot2)
library(gridExtra)
library(cowplot)
# 1st plot
p1 <- ggplot(data = new_data %>%
filter(Species =="Sharksucker_Remora")) +
scale_colour_manual(values=c(Sharksucker_Remora="black"), labels = c("Sharksucker Remora")) +
geom_line(mapping = aes(x = Date, y = Proportion, group = Species, colour = Species)) +
xlab("") +
ylab("Proportion") +
theme(legend.position="top") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + labs(colour = ~italic(M.alfredi)~"Hitchhiker Species:") +
theme(legend.key=element_blank())
# 2nd plot
p2 <- ggplot(data = new_data %>%
filter(Species !="Sharksucker_Remora")) +
geom_line(mapping = aes(x = Date, y = Proportion, group = Species, colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2", Red_Snapper="firebrick2", Juvenile_Remora="darkolivegreen3"), labels = c("Juvenile Remora", "Golden Trevally", "Red Snapper")) +
xlab("") + ylab("Proportion") + labs(colour = "") + theme(legend.position="top") + theme(legend.key=element_blank()) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
# 3rd plot
p3 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat =
'identity') +
xlab("Date (2015-2019)") + ylab("Total"~italic
(M.alfredi)~"Sightings") +
draw_plot_label(label =c("C") + theme(axis.text.x =
element_text(angle = 90, vjust = 0.5, hjust = 1))
# The grid
grid.arrange(p1,p2,p3)
I suggest you use labs(..., tag = ...) and theme(plot.tag = element_text()).
The code show how you can format the main title (here centered with hjust = 0.5) and the tag inside the theme() function. See the reproducible example, below:
# Packages
library(ggplot2)
library(gridExtra)
# library(cowplot) # not necessary here
# Plots
p1 <- ggplot() +
labs(title = "plot 1", tag = "A") +
theme(plot.title = element_text(hjust = 0.5),
plot.tag = element_text())
p2 <- ggplot() +
labs(title = "plot 2", tag = "B") +
theme(plot.title = element_text(hjust = 0.5),
plot.tag = element_text())
grid.arrange(p1, p2)
If you want the tag (A, B, C) to be inside the plotting area, this post suggest to use plot.tag.position = c(x, y). See for example:
p3 <- ggplot() +
labs(title = "plot 3", tag = "C") +
theme(plot.title = element_text(hjust = 0.5),
plot.tag = element_text(),
plot.tag.position = c(0.1, 0.8))
p3
Have you tried the package egg?
https://cran.r-project.org/web/packages/egg/vignettes/Overview.html
library(tidyverse)
library(magrittr)
data <- list()
for(i in 1:6) data[[i]] <- rnorm(100,0,1)
data %<>% bind_cols() %>% setNames(paste0("var",1:6))
p1 <- ggplot(data,aes(x = var1, y = var2)) + geom_point()
p2 <- ggplot(data,aes(x = var3, y = var4)) + geom_point()
p3 <- ggplot(data,aes(x = var5, y = var6)) + geom_point()
egg::ggarrange(p1,p2,p3,ncol = 1,
labels = c("A","B","C"))
Another option is using the patchwork package with plot_annotation which has the tag_levels argument which gives the possibility to add tags like letters or numbers. First a reproducible example with letters:
library(patchwork)
library(ggplot2)
p1 <- ggplot(mtcars) +
geom_point(aes(hp, disp)) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, mpg, group = gear)) +
ggtitle('Plot 2')
p1 + p2 & plot_annotation(tag_levels = 'A')
Created on 2022-08-21 with reprex v2.0.2
Another option with numbers where you change the tag_levels to "1" like this:
p1 + p2 & plot_annotation(tag_levels = '1')
Created on 2022-08-21 with reprex v2.0.2
As you can see, the tags have letters or numbers. Check the links above for more information and options.
question7 %>% ggplot(aes(x = year, y = n , group = state)) +
geom_point(aes(color = state)) + geom_smooth(method = "loess", formula
= y ~ x, level = 1, aes(color=state))+ labs(x = "Year", y = "No Visitors",#lab means labels title = "Number of Visitors by year by
state", # title of title making interpretation subtitle = "Yearly
comparison trend between NY and CA")
For the Y axis , i would like to label it as 1,2,3,4,5,6... all the way to 45.
Use scale_y_continuous() with the breaks = argument.
An example:
library(ggplot2)
p1 <- ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg))
p2 <- ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg)) +
scale_y_continuous(breaks = seq(10, 35, 1))
cowplot::plot_grid(p1, p2, ncol = 2)
Created on 2020-01-07 by the reprex package (v0.3.0)
I need to delete that symbol 'a' that is coming in the legend, plus I would like to know if there is a possibility to place the label on the top of the bars.
This my example file:
Residue,Position,Weight,SVM Count,Odd,Ttest,lower,upper,Resistance
G163R,163,0.357,49,19.9453848,6.978518E-82,5.6628402,70.2925768,Accessory
V165I,165,0.268,49,2.98167788,1.60934E-80,1.25797484,7.06728692,Novel
N155H,155,0.253,50,38.6089584,1.089188E-83,9.5815554,155.7070612,Major
library(ggplot2)
m <- read.csv('example.csv', header=T, row.names=1)
boxOdds = m$Odd
df <- data.frame(
yAxis = length(boxOdds):1,
boxnucleotide = m$Position,
boxCILow = m$lower,
boxCIHigh = m$upper,
Mutation = m$Resistance)
ticksy<-c(seq(0,0.3,by=.1), seq(0, 1, by =.5), seq(0, 20, by =5), seq(0, 150, by =50))
ticksx<-c(seq(0,300,by=25))
p <- ggplot(df, aes(x = boxnucleotide, y = boxOdds, colour=Mutation,label=rownames(m)))
p1 <- p + geom_errorbar(aes(ymax = boxCIHigh, ymin = boxCILow), size = .5, height = .01) +
geom_point(size = 1) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
scale_y_continuous(breaks=ticksy, labels = ticksy) +
scale_x_continuous(breaks=ticksx, labels = ticksx) +
coord_trans(y = "log10") +
ylab("Odds ratio (log scale)") +
scale_color_manual(values=c("#00BFC4","#F8766D","#619CFF")) +
xlab("Integrase nucleotide position") +
geom_text(size=4,hjust=0, vjust=0)+
theme(legend.position = c(0.9, 0.9))
p1
I already tried all possible solutions from Remove 'a' from legend when using aesthetics and geom_text but none worked out