I'm trying to do a plot with ggplot2 and geom_area. The fill is set by a variable. For some reason, only the 'outer' groups are filled. I can't figure out how to get the inner regions filled as well.
The same problem seems to occur here but no answer is given on how to solve it.
Below is an minimal example of the code i'm using and the resulting plot:
I'm using R 3.3 and ggplot2_2.1.0
Any help would be appreciated.
df <- data.frame(month = seq(from = as.Date("2016-01-01"), to = as.Date("2016-12-31"), by = "month"),
type = c(rep("past", times = 5), "current", rep("future", times = 6)),
amount = c(seq(from = 100, to = 1200, by = 100)))
df$type <- factor(df$type, levels = c("past", "current", "future"))
ggplot(data = df, aes(x = month, y = amount, fill = type)) +
geom_area()
I added 2 points in time arround the "current" value in order to produce an area. The problem is that with only one point no area can be drawn.
library(ggplot2)
df <- data.frame(month = seq(from = as.Date("2016-01-01"), to = as.Date("2016-12-31"), by = "month"),
type = c(rep("past", times = 5), "current", rep("future", times = 6)),
amount = c(seq(from = 100, to = 1200, by = 100)))
df <- rbind(df[1:5, ],
data.frame(month = as.Date(c("2016-05-15", "2016-06-15")),
type = c("current", "current"),
amount = c(550, 650)),
df[7:12, ])
df$type <- factor(df$type, levels = c("past", "current", "future"))
ggplot(data = df, aes(x = month, y = amount, fill = type)) +
geom_area()
Related
I want to create a timeline plot that roughly resembles the example below: lots of overlap at some points, not a lot of overlap at others.
What I need: overlapping images should repel each other where necessary, eliminating or reducing overlap. Ideally I'd be able to implement either a vertical or horizontal repel.
library(tidyverse)
library(ggimage)
test_img <- list.files(system.file("extdata", package="ggimage"), pattern="png", full.names=TRUE)
set.seed(123)
df <-
tibble(date = as.Date(paste0("2020-", round(runif(45, 1, 2)), "-", round(runif(45, 1, 10)))),
group = paste0("Timeline ", rep(1:9, each = 5)),
img = sample(test_img, size = 45, replace = T) )
df %>%
ggplot() +
geom_line(aes(x = date, y = group, group = group), size = 5, alpha = 0.2) +
geom_image(aes(x = date, y = group, image = img, group = group), asp = 1)
Something similar to the repelling in ggbeeswarm::geom_beeswarm or ggrepel::geom_text_repel would be nice, but those don't support images. So I think I need to pre-apply some kind of 1-dimensional packing algorithm, implementing iterative pair-wise repulsion on my vector of dates within each group, to try to find a non-overlapping arrangement.
Any ideas? Thank you so much!
Created on 2021-10-30 by the reprex package (v2.0.1)
Here is the solution I’ve been able to come up with, repurposing the circleRepelLayout function from the awesome packcircles package
into the repel_vector vector function that takes in your overlapping vector and a "repel_radius", and returns, if possible, a non-overlapping version.
I demonstrate the solution with the richtext geom since this is a geom I’ve always wished had repel functionality.
library(packcircles)
library(tidyverse)
library(ggtext)
library(ggimage)
repel_vector <- function(vector, repel_radius = 1, repel_bounds = range(vector)){
stopifnot(is.numeric(vector))
repelled_vector <-
packcircles::circleRepelLayout(x = data.frame(vector, ypos = 1, repel_radius),
xysizecols = c("vector", "ypos", "repel_radius"),
xlim = repel_bounds, ylim = c(0,1),
wrap = FALSE) %>%
as.data.frame() %>%
.$layout.x
return(repelled_vector)
}
overlapping_vec <- c(1, 1.1, 1.2, 10, 10.1, 10.2)
repelled_vec_default <- repel_vector(overlapping_vec)
repelled_vec_tighter <- repel_vector(overlapping_vec, repel_radius = 0.35)
ggplot() +
annotate("richtext", x = overlapping_vec, y = 3, label = "**test**", alpha = 0.5) +
annotate("richtext", x = repelled_vec_default, y = 2, label = "**test**", alpha = 0.5) +
annotate("richtext", x = repelled_vec_tighter, y = 1, label = "**test**", alpha = 0.5) +
scale_y_continuous(breaks = 1:3, labels = c("Tighter repel", "Default repel", "Overlapping points"))
In theory you apply this to 2D repelling as well.
To solve the problem in my question, this can be applied like so:
test_img <- list.files(system.file("extdata", package="ggimage"), pattern="png", full.names=TRUE)
set.seed(123)
df <-
tibble(date = as.Date(paste0("2020-", round(runif(45, 1, 2)), "-", round(runif(45, 1, 10)))),
group = paste0("Timeline ", rep(1:9, each = 5)),
img = sample(test_img, size = 45, replace = T) ) %>%
group_by(group) %>%
mutate(repelled_date = repel_vector(as.numeric(date),
repel_radius = 4,
repel_bounds = range(as.numeric(date)) + c(-3,3)),
repelled_date = as.Date(repelled_date, origin = "1970-01-01"))
df %>%
ggplot() +
geom_line(aes(x = date, y = group, group = group), size = 5, alpha = 0.2) +
geom_image(aes(x = repelled_date, y = group, image = img, group = group), asp = 1)
Created on 2021-10-30 by the reprex package (v2.0.1)
May I ask how can I distribute each of these four to two boxplots which contain the pulse meter of male and female.
islands = read.csv('Data.csv')
boxplot(islands$Pulse.meter.First..0m, islands$Pulse.meter.25m, islands$Pulse.meter.Second..0m, islands$Pulse.meter.25m.1)
Things like
boxplot(islands$Pulse.meter.25m ~ islands$Sex)
can distinguish them, but not working for four of them in the same time
before
Wanna boxplot like this
Here is an example using random data, since you hadn't provided data to download. The key is to first transform the data from the 'wide' format as you currently have the data, with a column per value, to a 'long' format, where all values are in the same column with an additional label column. Then the interaction function can be used to create an interaction between the pulse meter type and sex.
# example data with random values
islands <- data.frame(Sex = rep(c('Male', 'Female'), 15),
Pulse.meter.First..0m = rnorm(30, mean = 2),
Pulse.meter.25m = rnorm(30, mean = 1),
Pulse.meter.Second..0m = rnorm(30, mean = 3),
Pulse.meter.25m.1 = rnorm(30, mean = 4))
# reshape from wide to long
islands_long <- reshape(islands,
direction = "long",
varying = 2:5,
v.names = "value",
times = names(islands)[2:5],
timevar = 'measurement')
# plot the boxplot, 'cex.axis' decrease the font size so all the x-axis labels are visible
boxplot(value ~ interaction(Sex, measurement), data = islands_long, pars=list(cex.axis=0.5))
This generates:
library(ggplot2)
library(dplyr)
library(tidyverse)
df <- data.frame(
Gender = sample(c("Male", "Female"), 20, replace = TRUE),
Pulse.meter.First..0m = sample(10:60, 20, replace = FALSE),
Pulse.meter.25m = sample(30:60, 20, replace = FALSE),
Pulse.meter.Second..0m = sample(30:60, 20, replace = FALSE),
Pulse.meter.25m.1 = sample(10:60, 20, replace = FALSE)
)
df <- df %>%
group_by(Gender) %>%
pivot_longer(cols = Pulse.meter.First..0m:Pulse.meter.25m.1, names_to = "Pulse_meter", values_to = "Count") %>%
unite("Groups", Gender:Pulse_meter)
df$Groups <- factor(df$Groups, levels=c("Female_Pulse.meter.First..0m", "Male_Pulse.meter.First..0m",
"Female_Pulse.meter.25m","Male_Pulse.meter.25m",
"Female_Pulse.meter.Second..0m","Male_Pulse.meter.Second..0m",
"Female_Pulse.meter.25m.1","Male_Pulse.meter.25m.1"))
ggplot(data = df, aes(x= Groups, y = Count)) +
geom_boxplot() +
scale_x_discrete(labels=c("(F,0m)","(M,0m)","(F,25m)","(M,25m)", "(F,second_0m)", "(M,second_0m)",
"(F,25m.1)","(M,25m.1)")) +
labs(y="Counts") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
I have a plot like this:
Which was created with this code:
# Make data:
set.seed(42)
n <- 1000
df <- data.frame(values = sample(0:5, size = n, replace = T, prob = c(9/10, rep(0.0167,5))),
group = rep(1:100, each = 10),
fill2 = rep(rnorm(10), each = 100),
year = rep(2001:2010, times = 100)
)
df$values <- ifelse(df$year %in% 2001:2007 == T, 0, df$values)
# Plot
require(ggplot2)
p <- ggplot(data = df, aes(x = year, y = values, colour = as.factor(group))) + geom_line()
p
Since there are so many groups, the legend is really not helpfull.
Ideally I would like just two elements in the legend, one for group = 1 and for all the other groups (they should all have the same color). Is there a way to force this?
you can define a new variable that has only two values, but still plot lines according to their original group,
ggplot(data = df, aes(x = year, y = values, group = group,
colour = ifelse(group == 1, "1", "!1"))) +
geom_line() +
scale_colour_brewer("groups", palette="Set1")
I have panel data that that I want to visualize using ggplot2 such that each individual gets its own line and its color reflects the group that it is apart of. For example:
require(ggplot2)
set.seed(123)
frame <- data.frame(id = 1:6, month1 = sample(0:1, 6, replace = TRUE), month2 = sample(0:1, 6, replace = TRUE), month3 = sample(0:1, 6, replace = TRUE), group1 = rep(0:1, 3), group2 = rep(1:0, 3))
frame2 <- reshape(data = frame, direction = "long", idvar = "id", timevar = "time", varying = list(2:4))
ggplot(frame2, aes(x = time, y = month1, group = id, colour = id)) + geom_smooth()
In this plot, I would like each member of group1 to be red and each member of gruop2 to be blue and have each individual get its own line. Any idea on how to do this? Thanks.
You were close. You might consider jittering the lines as well if in your real application the Y axis variable is discrete.
ggplot(frame2, aes(x = time, y = month1, group = as.factor(id),
colour = as.factor(group2))) + geom_smooth()
I'm trying to "capture" some points within a bar.
The points represent 36 values on a monthly basis for 3 years.
The bars represent 3 values on a yearly basis for the same 3 years.
If you run the code you can see that some point of the first year are maybe captured by the bar of the second year and that the points of the 3rd year are "running out" of the last bar.
How can I align the bars and the points?
library(ggplot2)
set.seed(1)
df.year <- data.frame(yeardate = seq(as.Date("2010-01-01"), by = "year", length.out = 3), datevalue = abs(rnorm(3)))
df.month <- data.frame(monthdate = seq(as.Date("2010-01-01"), by = "month", length.out = 36), datevalue = abs(rnorm(36)))
df.month$inyear <- format(df.month$monthdate, "%Y")
df.month
p <- ggplot()
p <- p + geom_point(
data = df.month
,aes(x = monthdate, y = datevalue, color=inyear)
)
p <- p + geom_bar(
data = df.year
,aes(x = yeardate, y = datevalue)
,alpha=0.7
,stat = "identity"
)
p + scale_x_date(labels = date_format("%Y"), breaks = date_breaks("years"))
geom_bar is centering the bars on the dates given. Since the given dates are the first of the year, it is centered around the first of the year, and so much of 2012 lies outside the bar centered on 2012-01-01 (and much of that bar lies in 2011). So either center the bars in the middle of the year:
df.year <- data.frame(yeardate = seq(as.Date("2010-07-01"),
by = "year",
length.out = 3),
datevalue = abs(rnorm(3)))
which gives
or draw rectangles with the exact extent that you want them to be
df.year <- data.frame(yearstart = seq(as.Date("2010-01-01"),
by = "year", length.out = 3),
yearend = seq(as.Date("2010-12-31"),
by = "year", length.out = 3),
datevalue = abs(rnorm(3)))
and replace the geom_bar call with
p <- p + geom_rect(
data = df.year
,aes(xmin = yearstart, xmax = yearend,
ymin = 0, ymax = datevalue)
,alpha=0.7
)
giving