Plot graph with x-axis showing weeks and year - r

I have a dataframe that looks like this
dat <- data.frame(
weeks = c(23,24,25,26,23)
year = c(2022,2022,2022,2023,2023),
cases = c(70,98,69,430,56)
)
Now I am trying to plot this data using ggplot2
ggplot(data=dat, aes(x=factor(weeks), y=cases)) +
geom_bar(stat="identity")
I did like to have the x-axis split showing weeks in 2022 and weeks in 2023.
something like this
How can I do this?

I have figured it out, using facet_grid then place the group outside using stri.placement
ggplot(data=dat, aes(x=factor(weeks), y=cases)) +
geom_bar(stat="identity")+
facet_grid(. ~ year, scales = "free", switch = "x", space = "free_x") +
theme(strip.placement = "outside")

Related

Remove certain datapoints from boxplot in ggplot2

I have a box-plot of certain variables. I see the box plots of a number of days on x-axis (Sunday, Monday, Wednesday, Thursday and Friday). I would like to know how to remove two of the box plots from the graph. To be specific, I don't want there to be a box plot of Monday and Wednesday. The code I used was:
ggplot(contents2019, aes(x = days, y = time)) +
geom_boxplot() +
xlab("Day") +
ylab("Time") +
theme_bw()
You can filter your dataframe beforehand:
library(tidyverse)
contents2019 %>%
filter(!days %in% c("Monday", "Wednesday")) %>%
ggplot(aes(x = days, y = time)) +
geom_boxplot() +
xlab("Day") +
ylab("Time") +
theme_bw()

Plotting a bar chart with years grouped together

I am using the fivethirtyeight bechdel dataset, located here https://github.com/rudeboybert/fivethirtyeight, and am attempting to recreate the first plot shown in the article here https://fivethirtyeight.com/features/the-dollar-and-cents-case-against-hollywoods-exclusion-of-women/. I am having trouble getting the years to group together similarly to how they did in the article.
This is the current code I have:
ggplot(data = bechdel, aes(year)) +
geom_histogram(aes(fill = clean_test), binwidth = 5, position = "fill") +
scale_fill_manual(breaks = c("ok", "dubious", "men", "notalk", "nowomen"),
values=c("red", "salmon", "lightpink", "dodgerblue",
"blue")) +
theme_fivethirtyeight()
I see where you were going with using the histogram geom but this really looks more like a categorical bar chart. Once you take that approach it's easier, after a bit of ugly code to get the correct labels on the year columns.
The bars are stacked in the wrong order on this one, and there needs to be some formatting applied to look like the 538 chart, but I'll leave that for you.
library(fivethirtyeight)
library(tidyverse)
library(ggthemes)
library(scales)
# Create date range column
bechdel_summary <- bechdel %>%
mutate(date.range = ((year %/% 10)* 10) + ((year %% 10) %/% 5 * 5)) %>%
mutate(date.range = paste0(date.range," - '",substr(date.range + 5,3,5)))
ggplot(data = bechdel_summary, aes(x = date.range, fill = clean_test)) +
geom_bar(position = "fill", width = 0.95) +
scale_y_continuous(labels = percent) +
theme_fivethirtyeight()
ggplot

How to get complete, rather than partial, pie charts using gganimate

I have a problem when doing an animated pie chart with gganimate and ggplot.
I want to have normal pies each year, but my output is totally different.
You can see an example of the code using mtcars:
library(ggplot2)
library(gganimate)
#Some Data
df<-aggregate(mtcars$mpg, list(mtcars$cyl,mtcars$carb), sum)
colnames(df)<-c("X","Y","Z")
bp<- ggplot(df, aes(x="", y=Z, fill=X, frame=Y))+
geom_bar(width = 1, stat = "identity") + coord_polar("y", start=0)
gganimate(pie, "output.gif")
An this is the output:
It works well when the frame has only one level:
The ggplot code creates a single stacked bar chart with a section for every row in df. With coord_polar this becomes a single pie chart with a wedge for each row in the data frame. Then when you use gg_animate, each frame includes only the wedges that correspond to a given level of Y. That's why you're getting only a section of the full pie chart each time.
If instead you want a full pie for each level of Y, then one option would be to create a separate pie chart for each level of Y and then combine those pies into a GIF. Here's an example with some fake data that (I hope) is similar to your real data:
library(animation)
# Fake data
set.seed(40)
df = data.frame(Year = rep(2010:2015, 3),
disease = rep(c("Cardiovascular","Neoplasms","Others"), each=6),
count=c(sapply(c(1,1.5,2), function(i) cumsum(c(1000*i, sample((-200*i):(200*i),5))))))
saveGIF({
for (i in unique(df$Year)) {
p = ggplot(df[df$Year==i,], aes(x="", y=count, fill=disease, frame=Year))+
geom_bar(width = 1, stat = "identity") +
facet_grid(~Year) +
coord_polar("y", start=0)
print(p)
}
}, movie.name="test1.gif")
The pies in the GIF above are all the same size. But you can also change the size of the pies based on the sum of count for each level of Year (code adapted from this SO answer):
library(dplyr)
df = df %>% group_by(Year) %>%
mutate(cp1 = c(0, head(cumsum(count), -1)),
cp2 = cumsum(count))
saveGIF({
for (i in unique(df$Year)) {
p = ggplot(df %>% filter(Year==i), aes(fill=disease)) +
geom_rect(aes(xmin=0, xmax=max(cp2), ymin=cp1, ymax=cp2)) +
facet_grid(~Year) +
coord_polar("y", start=0) +
scale_x_continuous(limits=c(0,max(df$cp2)))
print(p)
}
}, movie.name="test2.gif")
If I can editorialize for a moment, although animation is cool (but pie charts are uncool, so maybe animating a bunch of pie charts just adds insult to injury), the data will probably be easier to comprehend with a plain old static line plot. For example:
ggplot(df, aes(x=Year, y=count, colour=disease)) +
geom_line() + geom_point() +
scale_y_continuous(limits=c(0, max(df$count)))
Or maybe this:
ggplot(df, aes(x=Year, y=count, colour=disease)) +
geom_line() + geom_point(show.legend=FALSE) +
geom_line(data=df %>% group_by(Year) %>% mutate(count=sum(count)),
aes(x=Year, y=count, colour="All"), lwd=1) +
scale_y_continuous(limits=c(0, df %>% group_by(Year) %>%
summarise(count=sum(count)) %>% max(.$count))) +
scale_colour_manual(values=c("black", hcl(seq(15,275,length=4)[1:3],100,65)))

Plot data from several columns of data frame using plyr and ggplot2

I have a data frame with 124 columns and observations. Part of it would be something like:
date <- c("2014-01-03", "2014-05-03","2014-02-04")
App <- c(0,2,4)
Email <- c(1,5,0)
Print <- c(0,0,1)
mgt <- c(1,9,12)
df<- data.frame (date, App, Email, Print, mgt)
I want to plot App against date, then Email against date, then Print against date etc in different plots. I am trying to use plyr and ggplot2 to output these plots and have come up with:
Plots <- function (Y){print(ggplot(df, aes(x=date, y= Y)) + geom_line() +
scale_x_date(breaks = date_breaks('month'), label= date_format('%b-%Y')) +
labs(title="A", x="Date Issued", y="Number of tickets issued")+
theme_bw()) }
ServicePlots <- d_ply (df, col , Plots, .print=TRUE)
The packages lubridate, chron and scales are also being used in the plots. However, this does not seem to work at all. Could someone please point out what I am doing wrong? And maybe help me out a bit?
Have no idea what plyr has to do with anything here just melt the data and plot it as is:
library(reshape2)
library(ggplot2)
library(scales)
df <- melt(df)
ggplot(df, aes(as.Date(date), value)) +
geom_line(aes(group = 1)) +
scale_x_date(breaks = date_breaks('month')) +
facet_wrap( ~ variable, scales = "free") +
labs(title="A", x="Date Issued", y="Number of tickets issued") +
theme_bw()
If you want it in a function form, do
Plots <- function(x){
x <- melt(x)
ggplot(x, aes(as.Date(date), value)) +
geom_line(aes(group = 1)) +
scale_x_date(breaks = date_breaks('month')) +
facet_wrap( ~ variable, scales = "free") +
labs(title="A", x="Date Issued", y="Number of tickets issued") +
theme_bw()
}
Plots(df)
Following is not ggplot based but it works:
par(mfrow=c(1,3))
for(i in 2:5)
plot(mydf[,1], mydf[,i], main=colnames(mydf)[i])

R + ggplot2 => add labels on facet pie chart [duplicate]

This question already has answers here:
ggplot, facet, piechart: placing text in the middle of pie chart slices
(4 answers)
Closed 7 years ago.
I want to add data labels on faceted pie char.
Maybe someone can can help me.
My data:
year <- c(1,2,1,2,1,2)
prod <- c(1,1,2,2,3,3)
quantity <- c(33,50,33,25,34,25)
df <- data.frame(year, prod, quantity)
rm(year, prod, quantity)
Code:
library(ggplot2)
# center's calculated by hand
centr2 <- c(16, 25, 49, 62.5, 81, 87.5)
ggplot(data=df, aes(x=factor(1), y=quantity, fill=factor(prod))) +
geom_bar(stat="identity") +
geom_text(aes(x= factor(1), y=centr2, label = df$quantity), size=10) +
facet_grid(facets = .~year, labeller = label_value) +
coord_polar(theta = "y")
And my result is:
If I remove coord_polar(theta = "y"), I will have the following plot:
And now it is clear for me, why my data labels did not match.
But I don't know how to fix it.
I read:
1. Place labels on Pie Chart
2. Add text to ggplot with facetted densities
3. Pie plot getting its text on top of each other
But didn't find the answer.
I would approach this by defining another variable (which I call pos) in df that calculates the position of text labels. I do this with dplyr but you could also use other methods of course.
library(dplyr)
library(ggplot2)
df <- df %>% group_by(year) %>% mutate(pos = cumsum(quantity)- quantity/2)
ggplot(data=df, aes(x=factor(1), y=quantity, fill=factor(prod))) +
geom_bar(stat="identity") +
geom_text(aes(x= factor(1), y=pos, label = quantity), size=10) + # note y = pos
facet_grid(facets = .~year, labeller = label_value) +
coord_polar(theta = "y")

Resources