Plotting non-overlapping levels of a factor on a circular plot using ggplot2, R
Tag : r , By : Brian Drum
Date : March 29 2020, 07:55 AM
it helps some times I am trying to create a circular plot to the display frequency/counts of months in my dataset but I would also like to group the months by season. Here is a similar plot for time of day, and now I would like to use the same approach to plot months/seasons. However, for some reason I can't seem to specify the right option to break my scale into non-overlapping month categories. Any suggestions are much appreciated. , Following simple version works: ggplot(eventdata, aes(x = factor(months), fill = season)) +
geom_histogram()+
coord_polar()
|
geom_bar ggplot2 plotting category but assigning colour scales based on an additional factor
Date : March 29 2020, 07:55 AM
hop of those help? Here's a solution as per the comment thread, using facets for type and colours for species. It may be less effective than this example in practice, depending on how many dates, types and species there are in the data. ggplot(df, aes(date, count)) +
geom_col(aes(fill = species)) +
facet_grid(. ~ type) +
scale_fill_brewer(palette = "Spectral") +
theme_light()
|
Reordering factor for plotting using forcats and ggplot2 packages from tidyverse
Tag : r , By : user121350
Date : March 29 2020, 07:55 AM
it helps some times First of all, thanks^13 to tidyverse. I want the bars in the chart below to follow the same factor levels reordered by forcats::fct_reorder (). Surprisingly, I see different order of levels in the data set when View ()ed as when they are displayed in the chart (see below). The chart should illustrate the number of failed students before and after the bonus marks (I want to sort the bars based on the number of failed students before the bonus). , Use factor with unique levels for your x -axis. ggplot (df) +
geom_bar (aes(factor(forcats::fct_reorder
(subject, FailNo, .desc= TRUE),
levels=unique(subject)),
FailNo,
fill = forcats::fct_rev (Bonus)),
position = 'dodge', stat = 'identity') +
theme(axis.text.x=element_text(angle=45, vjust=1.5, hjust=1.5, size = rel (1.2)))
|
R - ggplot2 - plotting one factor by two factors
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm following up on this link: ggplot2 bar plot with two categorical variables , We could use facet_wrap library(tidyverse)
df %>%
group_by_at(names(.)) %>%
summarise(n = n()) %>%
ggplot(., aes(x = Fruit, y = n, fill = Bug)) +
geom_bar(stat = "identity") +
facet_wrap(~ group)
Fruit <- c(rep("Apple",3),rep("Orange",5))
Bug <- c("worm","spider","spider","worm","worm","worm","worm","spider")
group <- rep(LETTERS[1:2], each = 4)
df <- data.frame(Fruit,Bug, group)
|
Why does ggplot2 ignore factor level order when plotting in this script?
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , For reasons that I don't really understand, the factor order is ignored when you use geom_point twice. Modifying the data so that you only need to call geom_point once fixed the problem. library(tidyverse)
df <- data.frame(
type = as.factor(c("Chain", "PTM", "PTM", "Motif", "Motif", "PTM", "Motif", "Chain", "PTM", "PTM", "Motif", "Motif")),
description = as.factor(c("seq", "methyl", "methyl", "RXL", "RXL", "amine", "CXXC", "seq", "amine", "methyl", "CXXC", "RXL")),
begin = c(1, 20, 75, 150, 67, 289, 100, 1, 124, 89, 73, 6),
order = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2),
length = c(300, 1, 1, 1, 1, 1, 1, 350, 1, 1, 1, 1)
)
#set desired order of factor levels
df <- df %>% mutate(
order = if_else(type == "PTM", true = order + 0.25, false = order),
description = factor(description, levels = c("amine", "methyl", "RXL", "seq", "CXXC")))
plot_start <- -100
plot_end <- 500
dfplot <- ggplot() +
xlim(plot_start, plot_end) +
scale_y_continuous(expand = c(0,0), limits =c(0, 2.5))
# white background
dfplot <- dfplot + theme_bw() +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank()) +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank()) +
theme(panel.border = element_blank())
#plot chains
dfplot <- dfplot + geom_rect(data= df[df$type == "Chain",],
mapping=aes(xmin=begin,
xmax=length,
ymin=order-0.2,
ymax=order+0.2),
colour = "blue",
fill = "#C4D9E9")
#plot motif positions
dfplot <- dfplot + geom_point(data = filter(df, type %in% c("PTM", "Motif")),
aes(begin, order, shape = description, color = description),
size = 3)
dfplot
|