Labeling two contours with direct.labels - r

I'm trying to compare two scalar fields and want to draw them in the same plot using contours labeling their values with directlabels.
The thing is, I'm not able to use two direct labels in the same plot.
Example:
library(ggplot2)
library(data.table)
library(directlabels)
grid <- expand.grid(lon = seq(0, 360, by = 2), lat = seq(-90, 0, by = 2))
grid$z <- with(grid, cos(lat*pi/180))
grid$z2 <- with(grid, sin(lat*pi/180))
grid.long <- melt(grid, id.vars = c("lon", "lat"))
# Manually adding two geom_dl's
ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "top.pieces", color = "black")
Only one variable is labeled.
Another way:
ggplot(grid.long, aes(lon, lat)) +
geom_contour(aes(z = value, color = variable)) +
geom_dl(aes(z = value, label = ..level.., color = variable),
stat = "contour", method = "top.pieces")
Any solution?
Thanks!

One solution is to provide different method= argument for the second geom_dl() call.
ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "bottom.pieces", color = "black")

Related

stat_summary() and fun.data = mean_sdl not working

set.seed(1) # generate random data
day1 = rnorm(20,0,1)
day2 = rnorm(20,5,1)
Subject <- rep(paste0('S',seq(1:20)), 2)
Data <- data.frame(Value = matrix(c(day1,day2),ncol=1))
Day <- rep(c('Day 1', 'Day 2'), each = length(day1))
df <- cbind(Subject, Data, Day)
Using this random data, I'd like to plot individual points with unique color for each subject and a summary point (mean + standard deviation).
It seems that the plot is okay when all points are plotted with the same color because stat_summary(fun.data = mean_sdl) works properly.
ggplot(data = df, mapping = aes(x= Day, y =Value)) +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 2),
geom = 'pointrange', fatten = 3*1.2, size = 1.2,
color= 'black') +
geom_point(size = 2)
But not when all points have unique color (for each subject).
ggplot(data = df, mapping = aes(x = Day, y = Value,
fill = Subject)) +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 2),
geom = 'pointrange', fatten = 3*1.2, size = 1.2,
color = 'black') +
geom_point(shape = 21, color = 'white', size = 2)
In your example ggplot assumes that each color corresponds to an individual group, but you want the grouping and color to be separate. Therefore, you need to explicitly define the group to be "Day".
ggplot(data = df, mapping = aes(x = Day, y = Value,
fill = Subject, group = Day)) +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 2),
geom = 'pointrange', fatten = 3*1.2, size = 1.2,
color = 'black') +
geom_point(shape = 21, color = 'white', size = 2)
Try the following:
ggplot(data = df, mapping = aes(x= Day, y =Value)) +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 2),
geom = 'pointrange', fatten = 3*1.2, size = 1.2,
color= 'black') +
geom_point(size = 2, aes(color = Subject))
Instead of specifying fill in aes() in the first line (ggplot(...)), I've moved it to the geom_point() element instead. Otherwise, stat_summary() will be doing its calculations grouped using Subject!

Change the representation of the graph

i would like to have help with my graph. I would like to be able to change the graph representation algorithm (LGL or other) but I can't. How can I do it? have tried several options that do not work...
library(NetworkToolbox)
library(dplyr)
library(igraph)
library(ggplot2)
library(ggnetwork)
M1 <- as_tibble(replicate(21,sample(1:3,100,rep=TRUE)))
colnames(M1) <- c("1st", "2nd", "3th", "4th", "5th", "6th","7th","8th","9th","10th",
"11th","12th","13th","14th","15th","16th","17th","18th","19th",
"20th","21th")
M2 <- as.matrix(round(cor(M1[,],method ="kendall"),2))
gr4ph <- graph.adjacency(M2, mode = "undirected",weight=TRUE)
MAST <- MaST(M2, normal = False)
gr4ph <- graph.adjacency(MAST , mode = "lower",weight=TRUE)
ggplot(gr4ph, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey", alpha = 1) +
geom_nodes(aes(color = name)) + theme_blank() +
geom_nodetext(aes(label = name), color = "black") +
geom_edgetext(aes(label = weight))+
theme(legend.position = "none")
You can use two solution. The layout argument is the key of the graph's representation :
The following exemple are coded with "Large graph layout"
With ggplot :
ggplot(gr4ph1, aes(x = x, y = y, xend = xend, yend = yend),layout = layout_with_lgl(gr4ph1))+
geom_edges(color = "grey", alpha = 1,size=1.5,) +
geom_nodes(aes(color = name), size = 3) + theme_blank() +
geom_nodetext(aes(label = name), color = "black", size = 3) +
geom_edgetext(aes(label = weight), size = 3,label.padding=unit(0.01, "lines"))+
theme(legend.position = "none")
With ggraph :
ggraph(gr4ph, layout = "lgl") +
geom_edge_link(aes(label = weight), angle_calc = 'along',label_dodge = unit(2.5, 'mm')) +
geom_node_point(aes(size=3,color= name)) +
theme(legend.position = "none")+
geom_node_text(aes(label = name), repel = TRUE)

Use a gradient color fill for a bubble grid chart

I've got a bubble grid chart created but I can't for the life of my change the colors of the fill. I want to use a rainbow gradient based on the values. Below is my code and I've attached image out my output
setwd("C:/Users/Schelly/Desktop/Projects/Jens_tables_and_figures_2020/Bubble_chart")
library(tidyverse)
library(reshape2)
pc <- read.csv("Para_Bubble_data2.csv", header = TRUE)
head(pc)
pcm<-melt(pc, id = c("Sample"))
pcm$Sample <- factor(pcm$Sample,levels=unique(pcm$Sample))
xx = ggplot(pcm, aes(x = Sample, y = variable)) +
geom_point(aes(size = value, fill = value), alpha = 0.75, shape = 21) +
scale_colour_gradientn(colours=rainbow(4))+
scale_size_continuous(limits = c(0.000001, 1), range = c(1,17), breaks = c(.01,.10,.50,.75)) +
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")
xx
Output of code
You need to specify aes(colour = value) if you want to use scale_color_gradientn:
library(ggplot2)
df <- data.frame(x = factor(rep(1:5, each = 6)),
y = factor(rep(1:6, 5)), val = sample(30))
ggplot(df, aes(x = x, y = y, size = val, colour = val)) +
geom_point() +
scale_color_gradientn(colours = c("red", "yellow", "blue"))
If you want to use fill (to preserve a different outline colour), you need to use scale_fill_gradientn:
ggplot(df, aes(x = x, y = y, size = val)) +
geom_point(aes(size = val, fill = val), alpha = 0.75, shape = 21) +
scale_fill_gradientn(colours = rainbow(4))+
labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")

Adding Coordinates to A Map created in R

I created a map of California subdivided with counties:
CAL17 <- ca_base +
theme_nothing() +
geom_polygon(data = ca_county, fill = NA, color = "yellow") +
geom_polygon(color = "black", fill = NA)
Now I am trying to add the coordinates.
Every time I try to add the coordinates the California map disappears and a single purple dot appears on an X and Y axis plot.
labs17 <- data.frame(lon17, lat17, stringsAsFactors = FALSE) +
ggplot() +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "White", size = 5) +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "yellow", size = 4)
CAL17 +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "black", size = 5) +
geom_point(data = ca_df, aes(x = lon17, y = lat17), color = "purple", size = 2)
and I get the following error
Error in FUN(X[[i]], ...) : object 'group' not found

Changing default color and removing legend in geom_density2d() plot

In the code below, how do I remove the 2nd legend (for alpha levels, I think) and change the default blue color to, say, red?
suppressMessages(library(ggmap))
data(crime)
houston.map <- get_map(location = geocode("Houston"),
zoom = 14)
ggmap(houston.map, extent = "device", legend = "topleft") +
stat_density2d(data = crime,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 2,
bins = 10,
geom = "polygon") +
labs(fill = "Density")
Thanks.
You can use the guides layer to remove the second legend by specifying alpha as FALSE and you can change the colour scale with scale_fill_gradient as I've done below:
ggmap(houston.map, extent = "device", legend = "topleft") +
stat_density2d(data = crime,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 2,
bins = 10,
geom = "polygon") +
scale_fill_gradient(low = "#333333", high = "#cc0000") +
labs(fill = "Density") +
guides(alpha = F)
Adding guides(alpha = F) removes that legend.
suppressMessages(library(ggmap))
data(crime)
houston.map <- get_map(location = geocode("Houston"),
zoom = 14)
ggmap(houston.map, extent = "device", legend = "topleft") +
stat_density2d(data = crime,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 2,
bins = 10,
geom = "polygon") +
guides(alpha = F) +
labs(fill = "Density")

Resources