Histogram of counts with faceting ggplot2 - r

Let's say I have the following data frame
category = c( "A", "B", "C", "B", "A", "B")
Jan.10 = c(20,10,30,20,15,46)
Feb.10 = c(10,10,40,20,25,46)
Mar.10 = c(40,10,80,20,25,56)
Jan.11 = c(50,10,20,20,45,56)
Feb.11 = c(70,10,30,20,35,46)
Mar.11 = c(60,10,50,20,25,66)
df <- data.frame(category, Jan.10, Feb.10, Mar.10, Jan.11, Feb.11, Mar.11)
How do I create a three histograms by faceting, one for each category, with months on the x-axis and counts on the y-axis?
Something like this I am trying to get:
example-image
This is my best attempt so far:
df_melt <- df %>% melt()
df_melt$variable <- as.yearmon(df_melt$variable, "%b.%y")
ggplot(df_melt, aes(x=variable)) +
geom_histogram() +
xlab("Year and Month") +
ylab("Sales") +
facet_grid(category ~.) +
theme_bw()
But it isn't working right.
Any help would be much appreciated!

I think you want something like this:
df_melt <- df %>% melt()
df_melt$variable <- as.yearmon(df_melt$variable, "%b.%y")
ggplot(df_melt, aes(x=variable, y=value)) +
geom_bar(stat='identity') +
xlab("Year and Month") +
ylab("Sales") +
facet_wrap(~category, ncol = 1) +
theme_bw()

Related

ggplot two side by side graphs with the same scale

I'm trying to create two side by side graphs to compare the values (one absolute values and one proportions). I managed to create some simple graphs, but I cannot figure out if I have to wrap them or use a grid? I just keep getting errors.
My data looks something like this:
recent_quarter <- c(12, 15, 2, 3)
all_data <- c(218, 323, 34, 12)
recent_perc <- c(38,47,6,9)
all_perc <- c(37,55,4,5)
gender <- factor(c("M", "F", "Unknown", "Other"),
levels = c("M", "F", "Unknown", "Other"))
df <- data.frame(gender, all_data, recent_quarter, all_perc,
recent_perc, all_data)
Then I created a simple plot
ggplot(df, aes(x = gender, y = recent_perc)) +
geom_col(fill = "gray70") +
theme_minimal()
For this one, I'd like to add a second plot with the all_perc as the y axis. I'm stumped on how to do this.
You could:
g1 <- ggplot(df, aes(x = gender, y = recent_perc)) +
geom_col(fill = "gray70") +
theme_minimal()
g2 <- g1 + aes(y=all_perc)
cowplot::plot_grid(g1,g2)
gridExtra (as referenced in #Josh's answer) and patchwork are two other ways to do the grid assembly.
Or:
library(tidyverse)
df <- data.frame(gender, all_data, recent_quarter, all_perc, all_data, recent_perc)
df_long <- df %>%
select(gender, ends_with("perc")) %>%
pivot_longer(-gender) ## creates 'name', 'value' columns
ggplot(df_long, aes(gender, value)) + geom_col() +
facet_wrap(~name)
install the package gridExtra and use:
grid.arrange(
ggplot(df, aes(x = gender, y = recent_perc)) +
geom_col(fill = "gray70") +
theme_minimal(),
ggplot(df, aes(x = gender, y = all_perc)) +
geom_col(fill = "gray70") +
theme_minimal(),
ncol = 2)

How can you plot `geom_point()` with `facet_wrap()` using per-group row number as x?

Is there a way to plot geom_point() so that it implicitly uses the row number as x in a facet? Just like plot(y) but also for multiple facets.
The following fails with Error: geom_point requires the following missing aesthetics: x:
df = data.frame(y = rnorm(60), group = rep(c("A", "B", "C"), 20))
ggplot(df, aes(y = y)) +
geom_point() +
facet_wrap(~group)
Naturally, you can do it using something like the following, but it is quite cumbersome.
df = df %>%
group_by(group) %>%
mutate(row = row_number())
ggplot(df, aes(x = row, y = y)) +
geom_point() +
facet_wrap(~group)
You can try this:
ggplot(df, aes(x=seq(y),y = y))+geom_point() + facet_wrap(~group)
In that way you can avoid the creation of an index variable as you mentioned!!!

Graph Help - Circular Barplot

I've no idea where to even start with this. I've looked at GGPlot and plotly etc to try and find the right thing but haven't come across anything.
This is as example of my data though
Skill <- c("Tackling", "Shooting", "Technique", "Passing", "Pace", "Stamina")
Grade <- c("A", "C", "C", "B", "A", "B")
data <- data.frame(Skill, Grade)
This is the sort of graph I'd like
I'm a football scout and it would be fantastic to be able to have a graph like that to compare the players we have to the player I'm scouting.
so if the grade is D, it would just show red, if the grade was C it would show red and orange. Etc.
This is quite close to what you want:
Skill <- c("Tackling", "Shooting", "Technique", "Passing", "Pace", "Stamina")
Grade <- c("A", "C", "C", "B", "A", "B")
data <- data.frame(Skill, Grade)
library(ggplot2)
library(dplyr)
data$grade <- factor(data$Grade, levels=c("D","C","B","A"))
data$grade2 <- recode(data$grade, A="B")
data$grade3 <- recode(data$grade2, B="C")
data$grade4 <- recode(data$grade3, C="D")
ggplot(data, aes(x=Skill, y=grade)) +
geom_bar(stat="identity", fill="green",col="black",width=1) +
geom_bar(aes(y=grade2),stat="identity", fill="yellow",col="black",width=1) +
geom_bar(aes(y=grade3),stat="identity", fill="orange",col="black",width=1) +
geom_bar(aes(y=grade4),stat="identity", fill="red",col="black",width=1) +
scale_y_discrete(limits = c("D","C","B","A")) +
coord_polar(start = pi/6) + theme_bw() + theme(axis.text.y = element_blank()) +
theme(axis.ticks = element_blank(), axis.title = element_blank())
How about this
library(ggplot2)
ggplot(data = data, aes(Skill, Grade, fill = Grade)) +
geom_tile() +
coord_polar() +
theme_bw()
To have all levels below the grade coded, you'll need to have all those lower levels within the dataframe, which is in a way redundant. Wouldn't it be?
d = transform(data, gr = as.numeric(factor(data$Grade, c("D", "C", "B", "A"))))
d = do.call(rbind, lapply(split(d, d$Skill), function(x){
foo = with(x, setNames(data.frame(Skill[1], Grade[1], seq(gr)), names(x)))
}))
library(ggplot2)
ggplot(d, aes(Skill, gr, fill = factor(gr, 4:1))) +
geom_col() +
coord_polar()

Keeping unit of measure in facet_wrap while scales="free_y"? [duplicate]

This question already has an answer here:
Setting individual y axis limits with facet wrap NOT with scales free_y
(1 answer)
Closed 4 years ago.
I'm trying to create a facet_wrap() where the unit of measure remains identical across the different plots, while allowing to slide across the y axis.
To clearify with I mean, I have created a dataset df:
library(tidyverse)
df <- tibble(
Year = c(2010,2011,2012,2010,2011,2012),
Category=c("A","A","A","B","B","B"),
Value=c(1.50, 1.70, 1.60, 4.50, 4.60, 4.55)
)
with df, we can create the following plot using facet_wrap:
ggplot(data = df, aes(x=Year, y=Value)) + geom_line() + facet_wrap(.~ Category)
Plot 1
To clarify the differences between both plots, one can use scale = "free_y":
ggplot(data = df, aes(x=Year, y=Value)) + geom_line()
+ facet_wrap(.~ Category, scale="free_y")
Plot 2
Although it's more clear, the scale on the y-axis in plot A isequal to 0.025, while being 0.0125 in B. This could be misleading to someone who's comparing A & B next to each other.
So my question right now is to know whether there exist an elegant way of plotting something like the graph below (with y-scale = 0.025) without having to plot two seperate plots into a grid?
Thanks
Desired result:
Code for the grid:
# Grid
## Plot A
df_A <- df %>%
filter(Category == "A")
plot_A <- ggplot(data = df_A, aes(x=Year, y=Value)) + geom_line() + coord_cartesian(ylim = c(1.5,1.7)) + ggtitle("A")
## Plot B
df_B <- df %>%
filter(Category == "B")
plot_B <- ggplot(data = df_B, aes(x=Year, y=Value)) + geom_line() + coord_cartesian(ylim = c(4.4,4.6)) + ggtitle("B")
grid.arrange(plot_A, plot_B, nrow=1)
Based on the info at Setting individual y axis limits with facet wrap NOT with scales free_y you can you use geom_blank() and manually specified y-limits by Category:
# df from above code
df2 <- tibble(
Category = c("A", "B"),
y_min = c(1.5, 4.4),
y_max = c(1.7, 4.6)
)
df <- full_join(df, df2, by = "Category")
ggplot(data = df, aes(x=Year, y=Value)) + geom_line() +
facet_wrap(.~ Category, scales = "free_y") +
geom_blank(aes(y = y_min)) +
geom_blank(aes(y = y_max))

Add multi-stack axes label to plot

I have a dataset, named “data”:
df=ddply(data,c("Treatment","Concentration"),summarise,mean=mean(Inhibition),sd=sd(Inhibition),n=length(Inhibition),se=sd/sqrt(n))
p <- ggplot(df, aes(x=Treatment, y=Inhibition))
p1 <- p + geom_bar(stat="identity", position="dodge") +
geom_errorbar(aes(ymin=Inhibition-se,ymax=Inhibition+se), position="dodge",width=0.2)
and I got the following graph:
I want x-axis to be like the picture below:
How woud I do this??
This is best achieved using a facet within ggplot. As you haven’t included a reusable dataset, I have made one here:
df <- data.frame(Group = c("A", "A", "A", "A", "B"),
SubGroup = c(letters[1:5]),
value = 1:5
)
See below the facet_grid line which has a few additional options specified. You can read more about the added arguments here
library(ggplot2)
ggplot(df, aes(x = SubGroup, value)) +
geom_bar(stat="identity", position="dodge") +
facet_grid(.~Group, scales = "free_x", space = "free", switch = "x") +
theme(strip.placement = "outside")
For your data, you will need to split the drug and dose into two separate columns first, like my example.

Resources