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:
Related
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
set.seed(3)
df <- data.frame(lambda = c(rep(0, 6), rep(1, 6), rep(1.5, 6)),
approach = rep(c(rep("A", 3), rep("B", 3)), 3),
value = rnorm(18, 0, 1))
ggplot(data = df, aes(x = lambda, y = value)) + geom_boxplot(aes(fill = approach))
I want to plot 3 sets of boxplots at lambda = 0, 1, and 1.5, respectively. Within each set are 2 boxplots, one corresponds to approach A and the other to approach B. However, the current code is only plotting two boxplots, whereas I'm looking for a total of six.
I think you want "lambda" to be a factor, e.g.
library(tidyverse)
set.seed(3)
df <- data.frame(lambda = c(rep(0, 6), rep(1, 6), rep(1.5, 6)),
approach = rep(c(rep("A", 3), rep("B", 3)), 3),
value = rnorm(18, 0, 1))
ggplot(data = df, aes(x = factor(lambda), y = value)) +
geom_boxplot(aes(fill = approach))
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)
My end goal is to create two outputs:
1) A static image showing all of my data, saved as a png
2) An animation of my data, saved as a gif.
I'm using ggplot2 and gganimate and I'm puzzled as to why the symbol size is not consistent between the two save methods.
I've tried adjusting the dpi and saving as jpg instead of png, but no luck. Can anyone help me figure out how to make the width, height, and symbol size in both output objects consistent?
Here's a reproducible example showing both outputs. You can see that the black points are smaller in the gif.
Make the png
library(gganimate)
library(ggplot2)
locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(1, 2, 3, 3.1, 3.2, 6),
LDT = c(1, 2, 3, 4, 5, 6))
g <- ggplot(locs, aes(x, y)) +
geom_point() +
theme_void() +
theme(plot.background = element_rect(fill = "pink"))
g
ggsave("test.png", g, width = 2, height = 2, dpi = 100)
Make the gif
anim <- g + transition_time(LDT)
animate(anim, duration = 1, fps = 20, width = 200, height = 200)
anim_save("test.gif")
animate() by default uses png() to generate frames.
In your ggsave call you specified a plot resolution of 100 dpi.
To get the same result using png you'll have to set res = 100 (see test_png_device.png).
Accordingly to have a consistent symbol size using animate you'll have to pass the resolution to png as an optional argument to animate as follows:
library(gganimate)
library(ggplot2)
library(gifski)
locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(1, 2, 3, 3.1, 3.2, 6),
LDT = c(1, 2, 3, 4, 5, 6))
g <- ggplot(locs, aes(x, y)) +
geom_point() +
theme_void() +
theme(plot.background = element_rect(fill = "pink"))
ggsave("test.png", g, width = 2, height = 2, dpi = 100)
png(filename = "test_png_device.png", width = 200, height = 200, units = "px", res = 100)
g
dev.off()
anim <- g + transition_time(LDT)
myAnimation <- animate(anim, duration = 1, fps = 20, width = 200, height = 200, renderer = gifski_renderer(), res = 100)
anim_save("test.gif", animation = myAnimation)
Addition: Not sure if you are interested in this, however, I like using library(plotly) for animations since it adds an animation slider by default.
Here is the ggplotly-way for your example:
library(plotly)
library(htmlwidgets)
locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
y = c(1, 2, 3, 3.1, 3.2, 6),
LDT = c(1, 2, 3, 4, 5, 6))
g <- ggplot(locs, aes(x, y)) + theme_void() +
theme(panel.background = element_rect(fill = "pink")) +
geom_point(aes(frame = LDT))
p <- ggplotly(g) %>%
animation_opts(500, easing = "linear", redraw = FALSE)
saveWidget(p, file = "myAnimation.html", selfcontained = TRUE)
browseURL("myAnimation.html")
Here a related post can be found.
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))