R: Creating ggplot histograms to mirror freq() function - r

I'm trying to use ggplot to create a bar plot (or histogram) to mirror the freq function from the descr package (where each discrete value in the variable gets its own column in the frequency plot, with the x-ais ticks centered around each value), but I'm having some trouble getting this to work.
Here is what I'm trying to create (but using ggplot so I can use its nice graphics):
library(ggplot2)
library(descr)
variable <- c(0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 7)
df <- data.frame(variable)
freq(df$variable)
And here is my trying (and failing) to do the same in ggplot:
histo.variable <- ggplot(df, aes(x = variable)) + # create histogram
geom_bar(stat = "bin") +
xlab("Variable Value") +
ylab("Count") +
scale_x_continuous(breaks = scales::pretty_breaks(n = 10))
histo.variable
As you can see, the bars are not centered on the tick marks. (Additionally, it'd be great to get rid of the little half-lines in between the bars.)
Thanks to anyone who can help!

Maybe like this:
ggplot(df, aes(x = variable)) +
geom_histogram(aes(y = ..density..),
binwidth = 1,
colour = "blue", fill = "lightblue")

Related

R: what are the bands of the bins in when using a geom_histogram in package ggplot2?

I have created a histogram plot using the following code:
p<-ggplot(df, aes(x=value)) +
geom_histogram(color="black", fill="white", binwidth=5)
p
My data ranges between 0 and 17. What are the bands of the bins e.g. is the 0 section 0-4? Is there a way of altering where the bands begin?
You can tell ggplot where you want one of the bins to be centered. For example, if you want bins between 0, 5, 10, etc you would do:
ggplot(df, aes(x = value)) +
geom_histogram(color = "black", fill = "white", binwidth = 5, center = 2.5)
Data
set.seed(1)
df <- data.frame(value = rpois(25000, 7))

gganimate barchart: smooth transition when bar is replaced

I want to create an animated barplot with the gganimate package. The barplot should contain 4 bars, but only three of the bars should be shown at the same time. When a bar drops out and a new bar comes in, the animation should be smooth (as it is when two bars switch position within the plot).
Consider the following example:
# Set seed
set.seed(642)
# Create example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
year = factor(sort(rep(2001:2005, 3))),
value = round(runif(15, 0, 100)),
group = c(letters[sample(1:4, 3)],
letters[sample(1:4, 3)],
letters[sample(1:4, 3)],
letters[sample(1:4, 3)],
letters[sample(1:4, 3)]))
# Load packages
library("gganimate")
library("ggplot2")
# Create animated ggplot
ggp <- ggplot(df, aes(x = ordering, y = value)) +
geom_bar(stat = "identity", aes(fill = group)) +
transition_states(year, transition_length = 2, state_length = 0)
ggp
If a bar is exchanged, the color of the bar just changes without any smooth animation (i.e. the new bar should fly in from the side and the replaced bar should fly out).
Question: How could I smoothen the replacement of bars?
I'm getting a little glitch at 2003 (b and c seem to swap upon transition), but hopefully this helps you get closer. I think enter_drift and exit_drift are what you're looking for.
library("gganimate")
library("ggplot2")
ggp <- ggplot(df, aes(x = ordering, y = value, group = group)) +
geom_bar(stat = "identity", aes(fill = group)) +
transition_states(year, transition_length = 2, state_length = 0) +
ease_aes('quadratic-in-out') + # Optional, I used to see settled states clearer
enter_drift(x_mod = -1) + exit_drift(x_mod = 1) +
labs(title = "Year {closest_state}")
animate(ggp, width = 600, height = 300, fps = 20)

ggplot reorders my factors

I am plotting a bar and line chart using a background theme from ggthemes. My variables are grouped by an ordered factor that I set. When I don't use the theme, the factors order the way I want them. But when I add a ggtheme, the order for the line changes, as can be seen in the legend. Why is this happening and how do I fix it?
Example code:
testCount %>%
ggplot(aes(x = tests)) +
theme_solarized_2(light = F) + scale_colour_solarized('blue') +
geom_bar(aes(y = ..prop.., fill = BandType), position = "dodge") +
stat_ecdf(aes(color = BandType), size = 1) +
scale_x_continuous(breaks = seq(0, 18, 1)) +
scale_y_continuous(breaks = seq(0, 1, 0.1), limits = c(0, 1), labels = percent)
Here is my desired output, where factors are ordered in bar and line chart:
And here is the undesired plot, where factor changes order in the line chart:
EDIT: adding theme_solarized_2(light = F) + scale_fill_solarized('blue') + scale_color_solarized('blue') made the factor ordering consistent. Thanks!

How can I add specific value to x-axis in ggplot2?

I am trying to make a graph in ggplot2. I want the x-axis to show 2.84 along with the sequence typed below. Is there any other way beside typing all the exact values in breaks()? I tried google but it doesn't solve my problem.
scale_x_continuous(limits = c(1, 7), seq(1,7,by=0.5), name = "Number of
treatments")
You can programmatically generate specific breaks, like this:
# make up some data
d <- data.frame(x = 6*runif(10) + 1,
y = runif(10))
# generate break positions
breaks = c(seq(1, 7, by=0.5), 2.84)
# and labels
labels = as.character(breaks)
# plot
ggplot(d, aes(x, y)) + geom_point() + theme_minimal() +
scale_x_continuous(limits = c(1, 7), breaks = breaks, labels = labels,
name = "Number of treatments")

Using ifelse to determine point size in r ggplot, works until legend is added

I am trying to set up a graph where the size of the point is smaller if n == 0 than n > 0. The code works until I add a legend. Here is my code that works:
ggplot(len.oo, aes(x = TCL, y = n, colour = worm, shape = worm)) + ylim(0, 20) +
geom_point(size = ifelse(len.oo$n == 0, 2, 4)) +
theme_bw() + xlab(expression(~italic("O. obscurus")~"TCL (mm)")) + ylab("Abundance") +
theme(legend.title=element_blank(), legend.position="none")
that gives me:
as soon as i add a legend it gives me an error. code with legend:
ggplot(len.oo, aes(x = TCL, y = n, colour = worm, shape = worm)) + ylim(0, 20) +
geom_point(size = ifelse(len.oo$n == 0, 2, 4)) +
theme_bw() + xlab(expression(~italic("O. obscurus")~"TCL (mm)")) + ylab("Abundance") +
theme(legend.title=element_blank(), legend.position=c(0.2, 0.8)) + guides(size=FALSE)
gives me the error:
Error: Aesthetics must be either length 1 or the same as the data (3):
size
I've also tried position = "top" and the like to have the legend outside the plot and without the guides(size=FALSE)
I could easily make the graph without the legend and then make the legend without the size difference and use other software to copy and paste the legend on the image, but I would like to do all this in R.
The problem lies here: geom_point(size = ifelse(len.oo$n == 0, 2, 4))
I can't give you a direct solution because no data was provided, but I'd suggest adding a variable in your len.oo dataframe so that this variable (let's say you call it size_n) is either a 2 or a 4 if n is equal to 0 and then change the ggplot layer to geom_point(aes(size = size_n)).
You can create this new variable with the following code:
library(dplyr)
len.oo <- mutate(len.oo, size_n = ifelse(n == 0, 2, 4))

Resources