In the example below, I'd like to colour values above 100 in a blue colour scheme (from light blue for the closest to 100 to dark blue for the max) and values below 100 in a warm colour range (from yellow for the closest to 100 to red for the min). See example of colour range below. Could someone kindly help me on that? I have tried a few different ways (incl. the one below) but unsuccessfully.Thanks a lot!
#library
library(raster)
library(ggplot2)
library(maptools)
data("wrld_simpl")
#sample raster
r <- raster(ncol=36, nrow=18)
r[] <- (-ncell(r)/2+1):(ncell(r)/2)
plot(r)
var_df <- as.data.frame(rasterToPoints(r))
#plotting
p <- ggplot()
p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
p <- p + coord_equal()
p <- p + scale_fill_gradient2(low = muted("red"), mid = "white",
high = muted("blue"), midpoint = 100)
p
Hmmm... so, do you actually want it to split exactly at layer=100?
If so,
#plotting
p <- ggplot()
p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
p <- p + coord_equal()
p <- p + scale_fill_gradientn(
colours=c("red", "yellow", "skyblue", "darkblue"),
values = rescale(c(min(var_df$layer),
100,
100.01,
max(var_df$layer))))
One option may be a personal ifelse function to set the colours
colour_func <- function(x){
ifelse(x$x > 150, 'darkblue',
ifelse(x$x > 130, 'cyan4',
ifelse(x$x > 110, 'cadetblue3',
ifelse(x$x > 100, 'cadetblue',
ifelse(x$x > 90, 'red',
ifelse(x$x > 60, 'darkorange3',
ifelse(x$x > 40, 'darkorange',
ifelse(x$x > 20, 'goldenrod2', "gold"))))))))
}
> #plotting
> p <- ggplot()
> p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
> p <- p + coord_equal()
> p <- p + scale_fill_gradientn(colours = colour_func(var_df))
> p
Related
I am trying to create Bland-Altman plots between 2 sets of percentages with a custom function that uses ggplot within it to generate the plot.
Perc1 <- sample(1:100, 100)
Perc2 <- sample(1:100, 100)
d <- data.frame(Perc1, Perc2)
bland <- function(dat, x, y){
df <- subset(dat[ ,c(x, y)])
df$avg <- rowMeans(df)
df$diff <- df[[1]] - df[[2]]
mean_diff <- mean(df$diff)
lower <- mean_diff - 1.96 * sd(df$diff)
upper <- mean_diff + 1.96 * sd(df$diff)
p <- ggplot(df, aes(x = avg, y = diff)) +
geom_point(size=2) +
geom_hline(yintercept = mean_diff) +
geom_hline(yintercept = lower, color = "red", linetype="dashed") +
geom_hline(yintercept = upper, color = "red", linetype="dashed") +
ggtitle("Bland-Altman Plot") +
ylab("Difference Between Measurements") +
xlab("Average Measurement")
plot(p)
}
bland(d, Perc1, Perc2)
However, when I run the function none of the lines are produced with the graph, but the title and x/y labels are. If anyone can explain why this is that would be great, thanks in advance.
Try this:
(Note also, the p <- and plot(p) are not needed as the function anyway returns the last object.)
library(tidyverse)
Perc1 <- sample(1:100, 100)
Perc2 <- sample(1:100, 100)
bland <- function(x, y){
df <- data.frame(x, y)
df$avg <- rowMeans(df)
df$diff <- df[[1]] - df[[2]]
mean_diff <- mean(df$diff)
lower <- mean_diff - 1.96 * sd(df$diff)
upper <- mean_diff + 1.96 * sd(df$diff)
p <- ggplot(df, aes(x = avg, y = diff)) +
geom_point(size=2) +
geom_hline(yintercept = mean_diff) +
geom_hline(yintercept = lower, color = "red", linetype="dashed") +
geom_hline(yintercept = upper, color = "red", linetype="dashed") +
ggtitle("Bland-Altman Plot") +
ylab("Difference Between Measurements") +
xlab("Average Measurement")
plot(p)
}
bland(Perc1, Perc2)
Created on 2022-05-17 by the reprex package (v2.0.1)
I would like to change the spread display of the legend bar as on the jpeg below. In example 1, I'd like to display the "100" threshold value in the middle of the legend bar. In example 2, I'd like to display the mean value (white colour) in the middle of the legend bar. I suspect both would require similar line of code. Could someone very kindly help me with this?
#library
library(raster)
library(ggplot2)
#sample raster
r <- raster(ncol=36, nrow=18)
r[] <- (-ncell(r)/2+1):(ncell(r)/2)
r[1,] <- 5000
plot(r)
var_df <- as.data.frame(rasterToPoints(r))
### example 1
p <- ggplot()
p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
p <- p + coord_equal()
p <- p + scale_fill_gradientn(
colours=c("red", "yellow", "skyblue", "darkblue"),
values = rescale(c(min(var_df$layer),
100,
100.01,
max(var_df$layer))))
p
### example 2
meanval <- mean(var_df$layer)
p <- ggplot()
p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
p <- p + coord_equal()
p <- p + scale_fill_gradient2(low = muted("red"), mid = "white",
high = muted("blue"), midpoint = meanval)
p
I want to plot pie charts using geom_scatterpie on top of a geom_tile plot. However, I am getting an error:
Error: Discrete value supplied to continuous scale
Here's the simple code that I cannot get to work:
library(ggplot2)
library(scatterpie)
nasafile <- "http://eosweb.larc.nasa.gov/sse/global/text/global_radiation"
nasa <- read.table(file=nasafile, skip=13, header=TRUE)
p <- ggplot(aes(y = Lat , x = Lon), data = nasa )+
geom_tile(aes(fill=Ann)) +
scale_fill_gradientn(colours=brewer.pal('YlOrRd', n=9)) +
theme_bw() +
coord_equal()
plot(p)
This works, but if I add the geom_scatterpie on top of that:
First the data for the pie charts to plot:
d <- data.frame(x=rnorm(5), y=rnorm(5))
d$A <- abs(rnorm(5, sd=1))
d$B <- abs(rnorm(5, sd=2))
d$C <- abs(rnorm(5, sd=3))
But I get the error when I do this:
p + geom_scatterpie(aes(x=x, y=y), data=d, cols=c("A", "B", "C")) + coord_fixed()
The problem is that your geom_tile uses a continuous fill scale while geom_scatterpie uses a discrete fill scale. It works if you change Ann to a factor. Not ideal, but this works:
nasa$Ann <- as.factor(as.integer(nasa$Ann))
mypalette <- brewer.pal(9, "YlOrRd") # 6 for geom_tile, 3 for geom_scatterpie
p <- ggplot(aes(y = Lat , x = Lon), data = nasa )+
geom_tile(aes(fill=Ann)) +
scale_fill_manual(values = mypalette) +
theme_bw() +
coord_equal()
p
d <- data.frame(x=rnorm(5, 0, 50), y=rnorm(5, 0, 30)) # larger sd
d$A <- abs(rnorm(5, sd=1))
d$B <- abs(rnorm(5, sd=2))
d$C <- abs(rnorm(5, sd=3))
p + geom_scatterpie(aes(x=x, y=y, r = 20), data=d, cols=c("A", "B", "C")) #larger radius
Or, using, size= instead of fill= (and no geom_scatterpie):
p <- ggplot(aes(y = Lat , x = Lon), data = nasa )+
geom_tile(aes(fill=Ann)) +
scale_fill_gradientn(colours=brewer.pal('YlOrRd', n=9)) +
theme_bw() +
coord_equal()
p
d <- data.frame(Lon = c(-100, 0, 100),
Lat = c(-50, 0, 50),
genvar = c(.1, .3, .5))
p + geom_point(data = d, aes(x = Lon, y = Lat, size = genvar),
color = "white")
I am using the ggplot function to plot this kind of graph
image
I want to add the specific value of the x-axis as shown in the picture
this is my code :
quantiles <- quantile(mat,prob = quant)
x <- as.vector(mat)
d <- as.data.frame(x=x)
p <- ggplot(data = d,aes(x=x)) + theme_bw() +
geom_histogram(aes(y = ..density..), binwidth=0.001,color="black",fill="white") +
geom_density(aes(x=x, y = ..density..),fill="blue", alpha=0.5, color = 'black')
x.dens <- density(x)
df.dens <- data.frame(x = x.dens$x, y = x.dens$y)
p <- p + geom_area(data = subset(df.dens, x <= quantiles), aes(x=x,y=y),
fill = 'green', alpha=0.6)
print(p)
I created a plot with several geom_area according to the following code :
library(ggplot2)
set.seed(1)
dat <- data.frame(matrix(rnorm(100, 10, 2), 100, 1))
dat_density <- data.frame(density(dat[, 1])[c("x", "y")])
quant <- quantile(dat[, 1], probs = seq(0, 1, 0.10))
library(RColorBrewer)
color_pal <- brewer.pal(length(quant)-1, "RdYlBu")
dens <- ggplot(data = dat_density, aes(x = x, y = y)) +
geom_line(size = 2)
for(i in 1:(length(color_pal))){
dens <- dens +
geom_area(data = subset(dat_density, x > quant[[i]] & x < quant[[i + 1]]), fill = color_pal[i])
}
dens
How can I add a common legend with each color of the color_pal vector (corresponding to all the 10% area of data) ?
The easiest way is to define the groups in your dataset
dat_density$quant <- cut(dat_density$x, breaks = c(-Inf, quant, Inf))
ggplot(data = dat_density, aes(x = x, y = y, fill = quant)) +
geom_line(size = 2) +
geom_area() +
scale_fill_brewer(palette = "RdYlBu")