I'm trying to loop through every column of the iris data set and plot a histogram in ggplot. So I'm expecting 5 different histograms to appear. However, my for loop below returns nothing. How can I fix this?
library(ggplot2)
for (i in colnames(iris)){
ggplot(iris, aes(x = i))+
geom_histogram()
}
Instead of using a for loop, the tidyverse/ggplot way would be to reshape the data from wide to long and then plot using facet_wrap
library(tidyverse)
iris %>%
gather(key, val, -Species) %>%
ggplot(aes(val)) +
geom_histogram(bins = 30) +
facet_wrap(~key, scales = "free_x")
Using dplyr, tidyr and ggplot:
library(ggplot2)
library(dplyr)
library(tidyr)
iris %>%
gather(Mesure, Value, -Species) %>%
ggplot(aes(x=Value)) + geom_histogram() + facet_grid(rows=vars(Species), cols=vars(Mesure))
Result:
Related
I would like to outline the shape of the graph produced by a group plot.
Using this code:
# Libraries
library(ggplot2)
library(babynames)
library(dplyr)
# Keep only 3 names
don <- babynames %>%
filter(name %in% c("Ashley", "Patricia", "Helen")) %>%
filter(sex=="F")
# Plot
don %>%
ggplot( aes(x=year, y=n, group=name, color=name)) +
geom_line()
I get this:
Is it possible to keep only the outline of the produced graph?
Example output:
There might be a ggplot2 way to do this but here one attempt using dplyr :
library(dplyr)
library(ggplot2)
don %>%
group_by(year) %>%
slice(which.max(n)) %>%
ggplot( aes(x=year, y=n, group=name, color=name)) +
geom_line()
The logic here is we keep only the row with max n value for each year so it removes all those lines which are being plotted below the outline line that we want.
Does anyone know how to use ggplot2 to convert a data frame in R with continous values into a pretty figure. This would be similar to the answer from this post but with ggplot2.
Is this possible?
New to R and ggplot2 so thanks in advance for any advice.
Here's an example using the mtcars data (scaled to give comparable values, so the numbers don't mean much).
The key things are the use of gather to tidy the data, geom_tile filled by value, and geom_text for the labels. Everything else is just manipulation of that particular data frame.
You could also just use one of the scale_fill_gradient geoms.
library(tidyverse)
library(viridis)
mtcars %>%
scale() %>%
as.data.frame() %>%
rownames_to_column(var = "make") %>%
gather(var, val, -make) %>%
ggplot(aes(var, make)) +
geom_tile(aes(fill = val)) +
geom_text(aes(label = round(val, 2)),
size = 3) +
coord_fixed() +
scale_fill_viridis() +
guides(fill = FALSE)
Or using:
+ scale_fill_gradient2(midpoint = 1.5)
I've got a grouped dataframe generated in dplyr where each group reflects a unique combination of factor variable levels. I'd like to plot the different groups using code similar to this post. However, I can't figure out how to include two (or more) variables in the title of my plots, which is a hassle since I've got a bunch of different combinations.
Fake data and plotting code:
library(dplyr)
library(ggplot2)
spiris<-iris
spiris$site<-as.factor(rep(c("A","B","C")))
spiris$year<-as.factor(rep(2012:2016))
spiris$treatment<-as.factor(rep(1:2))
g<-spiris %>%
group_by(site, Species) %>%
do(plots=ggplot(data=.) +
aes(x=Petal.Width)+geom_histogram()+
facet_grid(treatment~year))
##Need code for title here
g[[3]] ##view plots
I need the title of each plot to reflect both "site" and "Species". Any ideas?
Use split() %>% purrr::map2() instead of group_by() %>% do() like this:
spiris %>%
split(list(.$site, .$Species)) %>%
purrr::map2(.y = names(.),
~ ggplot(data=., aes(x=Petal.Width)) +
geom_histogram()+
facet_grid(treatment~year) +
labs(title = .y) )
You just need to set the title with ggtitle():
g <- spiris %>% group_by(site, Species) %>% do(plots = ggplot(data = .) +
aes(x = Petal.Width) + geom_histogram() + facet_grid(treatment ~
year) + ggtitle(paste(.$Species,.$site,sep=" - ")))
I am new to R
I would like plot using ggplot2's geom_bar():
top_r_cuisine <- r_cuisine %>%
group_by(Rcuisine) %>%
summarise(count = n()) %>%
arrange(desc(count)) %>%
top_n(10)
But when I try to plot this result by:
ggplot(top_r_cuisine, aes(x = Rcuisine)) +
geom_bar()
I get this:
which doesn't represent the values in top_r_cuisine. Why?
EDIT:
I have tried:
c_count=c(23,45,67,43,54)
country=c("america","india","germany","france","italy")
# sample Data frame #
finaldata = data.frame(country,c_count)
ggplot(finaldata, aes(x=country)) +
geom_bar(aes(weight = c_count))
you need to assign the weights in the geom_bar()
I want to plot a subset of my dataframe. I am working with dplyr and ggplot2. My code only works with version 1, not version 2 via piping. What's the difference?
Version 1 (plotting is working):
data <- dataset %>% filter(type=="type1")
ggplot(data, aes(x=year, y=variable)) + geom_line()
Version 2 with piping (plotting is not working):
data %>% filter(type=="type1") %>% ggplot(data, aes(x=year, y=variable)) + geom_line()
Error:
Error in ggplot.data.frame(., data, aes(x = year, :
Mapping should be created with aes or aes_string
Thanks for your help!
Solution for version 2: a dot . instead of data:
data %>%
filter(type=="type1") %>%
ggplot(., aes(x=year, y=variable)) +
geom_line()
I usually do this, which also dispenses with the need for the .:
library(dplyr)
library(ggplot2)
mtcars %>%
filter(cyl == 4) %>%
ggplot +
aes(
x = disp,
y = mpg
) +
geom_point()
During typing with piping if you reenter the data name as you have as I shown with bold below, function confuses the sequence of arguments.
data %>% filter(type=="type1") %>% ggplot(***data***, aes(x=year, y=variable)) + geom_line()
Hope it works for you.