ggplot letters in x axis replacing a continuos numerical sequence - r

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

Related

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:

How to animate the axis label using `gganimate`?

I am actually very amazed to see I cannot quickly find a guide to how to do this. Here is an example:
library(ggplot2)
library(gganimate)
library(data.table)
library(magrittr)
dt <- lapply(seq(10), function(i){
mean = i
label = paste0("T = ", i)
dt = data.table(x = seq(0, 50, length.out = 100))
set(dt, j = "y", value = dt[, dlnorm(x, meanlog = log(mean), sdlog = 0.2)])
set(dt, j = "frameN", value = i)
return(dt)
}) %>% rbindlist
print(dt)
p <- ggplot(dt, aes(x = x, y = y)) +
geom_line() +
scale_x_continuous(name = "x", breaks = c(0, 1)) +
transition_manual(frameN)
animate(p)
I want the breaks and labels of scale_x_continuous to follow my own definitions:
arr_breaks <- c(1, 3, 2, 4, 3, 5, 4, 6, 5, 7)
arr_labels <- paste0(seq(10, 100, 10), " kg")
And then
breaks = arr_breaks[1], labels = arr_labels[1] for frame 1
breaks = arr_breaks[2], labels = arr_labels[2] for frame 2
...
breaks = arr_breaks[10], labels = arr_labels[10] for frame 10
No matter how I do it I got errors. Any idea?
As #z-lin noted, gganimate is not currently set up (to my knowledge) to animate scales with different breaks. The effect could be closely approximated using geoms, and with some more work you could probably make an exact visual match to a changing scale.
breaks_df <- data.frame(
frameN = c(1:10),
arr_breaks = c(1, 3, 2, 4, 3, 5, 4, 6, 5, 7),
arr_labels = paste0(seq(10, 100, 10), " kg")
)
p <- ggplot(dt, aes(x = x, y = y)) +
geom_segment(data = breaks_df, color = "white",
aes(x = arr_breaks, xend = arr_breaks,
y = -Inf, yend = Inf)) +
geom_text(data = breaks_df, vjust = 3, size = 3.5, color = "gray30",
aes(x = arr_breaks, y = 0, label = arr_labels)) +
geom_line() +
scale_x_continuous(name = "x", breaks = c(0)) +
coord_cartesian(clip = "off") +
transition_manual(frameN)
animate(p, width = 600, height = 250)

ggplot adjust size of legend key values

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))

Resources