Trying to use ggpattern for this plot but can't get it to work right. Legend looks okay doesn't translate to what's on plot itself. Not stripes or dots on actual plot?
test <- tibble(names = c("fred", "harry", "tom"),
start = c(1, 3, 5),
end = c(10, 5, 7),
stripe = c("yes", "no", "yes"))
ggplot()
geom_rect_pattern(data = test,
aes(xmin = names,
xmax = names,
ymin = start,
ymax = end,
color = names,
fill = names,
pattern = stripe), size = 4)
CodePudding user response:
Your issue is that you do not have xmin
, xmax
, ymin
, ymax
values. Since you use rectangles (you need to specify 4 corners): e.g:
plot_df <- data.frame(
xmin = c(0, 10, 3),
xmax = c(8, 18, 4),
ymin = c(0, 10, 8),
ymax = c(5, 19, 15),
type = c('a', 'b', 'c'),
colour1 = c('red', 'black', 'blue')
)
After that
ggplot(plot_df)
geom_rect_pattern(
aes(
xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax,
pattern_fill = I(colour)
),
pattern = 'stripe',
colour = 'black',
pattern_density = 0.3,
fill = NA
)
theme_bw(18)
CodePudding user response:
To produce the plot without altering your data, you could try:
ggplot()
geom_rect_pattern(data = test,
aes(xmin = as.numeric(factor(names)) - 0.25,
xmax = as.numeric(factor(names)) 0.25,
ymin = start,
ymax = end,
fill = names,
pattern = stripe), pattern_fill = 'black', size = 0)
scale_x_continuous(breaks = seq(length(levels(factor(test$names)))),
labels = levels(factor(test$names)))
scale_pattern_manual(values = c('none', 'stripe'))