ggplot adjust size of legend key values - r

I am having problems finding a way to adjust the value of key legends. In the example below count ranges from 3 to 500, however the legend only ranges from 100 to 500. This is understandable, though I would like to change the values of the legend so there is a size that corresponds with a count of 3.
So in sum I would like to find a way to adjust the key values to correspond with count values I select. Is this possible?
library(ggplot2)
df <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(4, 2, 6, 1, 7, 7),
count = c(3, 100, 200, 300, 400, 500))
plt <- ggplot() +
geom_point(data = df,
aes(x = x, y = y, size = count))

Credit to this answer goes to aosmith.
Below is the correct code.
library(ggplot2)
df <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(4, 2, 6, 1, 7, 7),
count = c(3, 100, 200, 300, 400, 500))
plt <- ggplot() +
geom_point(data = df,
aes(x = x, y = y, size = count)) +
scale_size_continuous(breaks = c(3, 100, 200, 500))

Related

Change panel width ggplot2

I am creating some plots with ggplot2 and I would like to add stats to it post-hoc. However, I have the issue that the plot ends so close to the margin that I don't have space to add it. Here is a picture:
I would like to draw the stars etc at the right hand side of each plot. So I would like to just have some extra space at the right hand side of each panel. I have been trying to find a command for that but it hasn't been going well...
Here is the code I'm using to plot:
df %>%
group_by(group, animal, day, zone) %>%
summarise(sum_cum = sum(cum_time_sec)) %>%
ggline(x='day', y='sum_cum', color = 'group', fill = ' group', size = 1.1, outlier.shape = NA,
add = c("mean_se"), add.params = list(alpha=0.5), point.size = 4, linetype = 1, facet.by = "zone", ncol = 4)
Thank you!
Without data, it's always more difficult to show a working solution, but we can replicate your plot approximately by doing:
library(ggpubr)
library(dplyr)
df <- data.frame(group = rep(c('A', 'B'), each = 20),
animal = 1, day = rep(1:5, 8), zone = rep(rep(1:4, each = 5), 2),
cum_time_sec = c(5, 10, 11, 10, 8, 10, 9, 8, 7, 8, 40, 35, 37,
39, 41, 5, 4, 3, 4, 5, 4, 4, 4, 5, 3, 10, 6, 7,
8, 6, 40, 45, 44, 45, 49, 4, 3, 4, 2, 1))
p <- df %>%
group_by(group, animal, day, zone) %>%
summarise(sum_cum = sum(cum_time_sec)) %>%
ggline(x='day', y='sum_cum', color = 'group', fill = ' group',
size = 0.5, outlier.shape = NA,
add = c("mean_se"), add.params = list(alpha=0.5),
point.size = 1, linetype = 1, facet.by = "zone", ncol = 4) +
scale_color_manual(values = c('#1064bc', '#afafaf'))
p
To increase panel spacing, we simply use the panel.spacing.x theme element:
p + theme(panel.spacing.x = unit(15, 'mm'))
If you want extra space after the last point, simply change the x axis limits:
p + scale_x_continuous(breaks = 1:5, limits = c(1, 7))
Another option is to use multiple expansions in scale_continuous.
## I am using Allan's carefully produced plot `p`.
## Thanks, Allan +1
p +
scale_x_continuous(breaks = 1:5, expand = expansion(mult = c(0, .4)))

ggplot letters in x axis replacing a continuos numerical sequence

I would like to replace the numbers on the X-axis with a string.
df <- data.frame(Start = c(1, 3, 8, 10),
End = c(5, 10, 12, 15),
Height = c(10, 4, 5, 6))
p <- ggplot(df)+
geom_segment(aes(x = Start,
xend = End,
y = Height,
yend = Height))
This is the string, each letter should go in a distance of 1:
The first segment Will have coverage of ADFLK
lettersString <- 'ADFLKENACOINQLWEJI'
This is what I would like to obtain.
I have tried solving it like this: (It doesn't work)
p + theme(axis.text.x = strsplit(lettersString, split = "+"))
library(ggplot2)
df <- data.frame(Start = c(1, 3, 8, 10),
End = c(5, 10, 12, 15),
Height = c(10, 4, 5, 6))
xAxis <- unlist(strsplit(lettersString, split = "+"))
p <- ggplot(df)+
geom_segment(aes(x = Start,
xend = End,
y = Height,
yend = Height)) +
scale_x_continuous(labels=xAxis, breaks=1:length(xAxis), limits=c(1,length(xAxis)))
p

How to add edges/borders to the links in geom_link2 in R?

Is there a way to add an edge/border (not sure of the proper word) to the links created using ggforce::geom_link2 in R? Something similar to points with pch >20.
The issue that I see is that geom_link2 uses col instead of fill in order to define the colour of the link. Therefore I am not sure how the colour of the border could be defined. In turn that makes me think that there is no way to make a border on the link.
Any idea?
Thanks.
EDIT 10/02/21: follow up of the solution from #tjebo.
Here is a reproducible example of the path-crossing issue. The border disappears at the crossing. With 2 paths it is still ok to visualise, but in a complex ordination it gets very messy.
library(ggforce)
#> Loading required package: ggplot2
df <- data.frame( x = c(5, 10, 5, 10), y = c(5, 10, 10, 5), width = c(1, 10, 6, 2), colour = letters[1:4], group = c(1, 1, 2, 2))
ggplot(df) +
geom_path(aes(x = x, y = y, group = group), size = 10, lineend = 'round') +
geom_link2(aes(x = x, y = y, colour = colour, group = group),
size = 5, lineend = 'round', n = 500)
Created on 2021-02-10 by the reprex package (v1.0.0)
Cheeky workaround: Create two geom_link2 plots overlaid. If you want a simple unicolor border, you can as well (and better) use geom_path instead.
Adapted from the example in ?geom_link.
library(tidyverse)
library(ggforce)
lines <- data.frame( x = c(5, 12, 15, 9, 6), y = c(17, 20, 4, 15, 5), xend = c(19, 17, 2, 9, 5), yend = c(10, 18, 7, 12, 1), width = c(1, 10, 6, 2, 3), colour = letters[1:5])
ggplot(lines) +
geom_path(aes(x = x, y = y, group = 1), size = 10, lineend = 'round') +
geom_link2(aes(x = x, y = y, colour = colour, group = 1),
size = 5, lineend = 'round', n = 500)
Created on 2021-02-06 by the reprex package (v0.3.0)

How to remove the default grey fill for linetype legend in barplot with ggplot2?

I have a bar-plot with two different variables.
For one of the factors (gr) I have chosen different ´lintype´ in the plot.
The legend for "gr" shows ´lintype´ but with a dark grey fill, which I think is confusing.
Does anyone know how to remove the fill or change it to white or transparent?
(All tips I have found only change a background to the legend, but does not affect the grey fill)
yval <- c(3, 7, 4, 4, 8, 9, 4, 7, 9, 6, 6, 3)
trt <- rep(c("A", "B", "C"), times=4)
gr <- rep(c(rep(("case"), times = 3), rep(("control"), times = 3)), times = 2)
var <- c(rep(("var1"), times = 6), rep(("var2"), times = 6))
df <- data.frame(yval, device, ccgroup, var)
ggplot(data=df, aes(x=var)) +
geom_bar( color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge")
This can be achieved e.g. via guide_legend which allows you to set the fill color used in the legend. Try this:
library(ggplot2)
yval <- c(3, 7, 4, 4, 8, 9, 4, 7, 9, 6, 6, 3)
trt <- rep(c("A", "B", "C"), times=4)
gr <- rep(c(rep(("case"), times = 3), rep(("control"), times = 3)), times = 2)
var <- c(rep(("var1"), times = 6), rep(("var2"), times = 6))
df <- data.frame(yval, trt, gr, var)
ggplot(data=df, aes(x=var)) +
geom_bar(color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge") +
guides(linetype = guide_legend(override.aes = list(fill = c(NA, NA))))
#> Warning: Ignoring unknown aesthetics: weights

Using geom_ridgeline with a log y-axis

I am trying to visualise timeseries data, and thought the ggridges package would be useful for this. However some of my data needs to be plotted on a log-scale. Is there a way to do this?
I tried it using y = 0.001 instead of 0, as y = zero fails, but then the heights are not correct. This can be seen when you plot the points as well.
Thanks
Example below:
data <- data.frame(x = 1:5, y = rep(0.001, 5), height = c(0.001, 0.1, 3, 300, 4))
ggplot(data) +
geom_ridgeline(aes(x, y, height = height),fill = "lightblue") +
scale_y_log10() +
geom_point(aes(x=x, y=height))
Hopefully this will give you a lead towards solving your problem.
Using an example from ggridges (https://wilkelab.org/ggridges/articles/introduction.html), I added +1 to avoid zeros (and thus Inf) when taking log10
library(ggridges)
d <- data.frame(
x = rep(1:5, 3),
y = c(rep(0, 5), rep(1, 5), rep(2, 5)),
height = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1)
)
ggplot(d, aes(x, (y + 1), height = height, group = y)) +
geom_ridgeline(fill = "lightblue")+
scale_y_log10() +
annotation_logticks(sides = "l")
Generates:

Resources