Apply a Ggplot-Function Per Group with Dplyr and Set Title Per Group

Apply a ggplot-function per group with dplyr and set title per group

Use .$Species to pull the species data into ggtitle:

iris %>% group_by(Species) %>% do(plots=ggplot(data=.) +
aes(x=Petal.Width, y=Petal.Length) + geom_point() + ggtitle(unique(.$Species)))

R: Using dplyr group_by with ggplot2 and changing title per group

Use split and map. To save the plots as png you can include ggsave.

library(tidyverse)

df_summarised %>%
group_split(Isotope,year) %>%
map(~{
plots = ggplot(data=.x, aes(x=month,y=TotalMonthlyExcretion)) +
geom_bar(stat="identity", width = .5, fill="tomato3") +
labs(title = paste("Liquid Radioactive Waste. Year: ", .x$year[1]," Isotope: ", .x$Isotope[1]),
x = "Month",
y = "Total Monthly Excretion (MBq)",
caption = paste("Year: ",.x$year[1])
) +
theme_bw() +
theme(
plot.title = element_text(color="black", size=14, face="bold.italic"),
axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"))

ggsave(sprintf('plots%s_%s.png', .x$Isotope[1], .x$year[1]), plots)
})

Set ggplot title to reflect dplyr grouping

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) )

Sample Image

applying a function to the output of dplyr's group_by

Well, you have a parenthesis problem and a file naming problem so maybe it's one of those that you are referring to. I'm assuming

iris %>%
group_by(Species) %>%
do({
p <- ggplot(., aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()
ggsave(p, filename=paste0(unique(.$Species),".pdf"))
})

would fix your problem.

Get title for plots when using purrr and ggplot with group_by and nest()

Making a data frame column of ggplot objects is a little unusual and cumbersome, but it can work if that's what suits the situation. (It seemed like geom_bar didn't actually make sense for this data, so I switched to histograms on a filtered subset of carbs).

With your method, you create the column of plots, save the data frame into a variable, then map2 across the list-column of plots and the vector of carbs, adding a title to each plot. Then you can print any item from the resulting list, or print them all with map or walk.

library(tidyverse)

df <- mtcars %>%
filter(carb %in% c(1, 2, 4)) %>%
mutate(carb = as.character(carb))

df_with_plots <- df %>%
group_by(carb) %>%
nest() %>%
mutate(plot = map(data, function(.x) {
.x %>%
ggplot() +
geom_histogram(aes(mpg))
}))

plots1 <- map2(df_with_plots$plot, df_with_plots$carb, ~(.x + labs(title = .y)))

# plots1[[1]] # would print out first plot in the list

walk(plots1, print)

Sample Image

Removed additional plots for the sake of brevity

The other option, which seems more straightforward, is to split the data frame into a list of data frames, then create plots however you need for each of those.

A few advantages: calling split gives each value of carb as the name of the corresponding list item, which you can access easily with imap, and which carries over to the plots2 list (see below how I printed a plot by name). You can also do this in a single step. I also have a hard time working with nested data, because I'd prefer to be able to see the data frames, which you can do by printing out the split list of data frames.

plots2 <- df %>%
split(.$carb) %>%
imap(function(carb_df, carb) {
ggplot(carb_df) +
geom_histogram(aes(mpg)) +
labs(title = carb)
})

plots2[["4"]]

Sample Image

As with the first method, you can print out all the plots with walk(plots2, print).

r cumsum per group in dplyr

Ah. After fiddling around I seem to have found it.

pdf = df %>% group_by(group) %>% arrange(dates) %>% mutate(cs = cumsum(sales))

Output with forloop in question:

> pdf = data.frame(dates=as.Date(as.character()), group=as.character(), sales=as.numeric())
> for(grp in unique(df$group)){
+ subs = filter(df, group == grp) %>% arrange(dates)
+ pdf = rbind(pdf, data.frame(dates=subs$dates, group=grp, sales=subs$sales, cs=cumsum(subs$sales)))
+ }
> pdf
dates group sales cs
1 2014-01-02 A -0.56047565 -0.5604756
2 2014-01-03 A -0.23017749 -0.7906531
3 2014-01-04 A 1.55870831 0.7680552
4 2014-01-05 A 0.07050839 0.8385636
5 2014-01-06 A 0.12928774 0.9678513
6 2014-01-02 B 1.71506499 1.7150650
7 2014-01-03 B 0.46091621 2.1759812
8 2014-01-04 B -1.26506123 0.9109200
9 2014-01-05 B -0.68685285 0.2240671
10 2014-01-06 B -0.44566197 -0.2215949
11 2014-01-02 C 1.22408180 1.2240818
12 2014-01-03 C 0.35981383 1.5838956
13 2014-01-04 C 0.40077145 1.9846671
14 2014-01-05 C 0.11068272 2.0953498
15 2014-01-06 C -0.55584113 1.5395087

Output with this line of code:

> pdf = df %>% group_by(group) %>% mutate(cs = cumsum(sales))
> pdf
Source: local data frame [15 x 4]
Groups: group

dates group sales cs
1 2014-01-02 A -0.56047565 -0.5604756
2 2014-01-03 A -0.23017749 -0.7906531
3 2014-01-04 A 1.55870831 0.7680552
4 2014-01-05 A 0.07050839 0.8385636
5 2014-01-06 A 0.12928774 0.9678513
6 2014-01-02 B 1.71506499 1.7150650
7 2014-01-03 B 0.46091621 2.1759812
8 2014-01-04 B -1.26506123 0.9109200
9 2014-01-05 B -0.68685285 0.2240671
10 2014-01-06 B -0.44566197 -0.2215949
11 2014-01-02 C 1.22408180 1.2240818
12 2014-01-03 C 0.35981383 1.5838956
13 2014-01-04 C 0.40077145 1.9846671
14 2014-01-05 C 0.11068272 2.0953498
15 2014-01-06 C -0.55584113 1.5395087

How a simpler derive is written in R by group (in R, ggplot, dplyr, tidyverse)?

As I understood, you want calc to be computed using the current and previous t and date_heure values. To get the value of a previous row in a particular column, you can use lag, as follows:

datas<- (datas
%>% mutate (
calc = (t - lag(t)) / as.integer((date_heure - lag(date_heure)))
)
)

Please note that the value of calc for the first row is going to be NA. Hence, you may need to skip and give it a default value before you plot your figure.

For example:

datas <- datas[-1,]  # To skip the first `NA` value
datas[1,]$calc <- 0 # To give it a default value of `0`

Hope it helps.



Related Topics



Leave a reply



Submit