From a database called "datoschile" that incorporates 6 variables, I have created a panel data called "magrupados3" that uses the variable "Periodo" as an identity variable and I have filtered thstrong texte information using only "Importaciones" and "Exportaciones" variables
This the excel file needed to work with this code
https://gitlab.com/pedritoandrade/how-can-i-make-a-transition-to-a-linear-model
datos <- read.xlsx("C:\\Users\\PEDRO ANDRADE 2019\\Desktop\\Curso Betametrica\\Analisis exploratorio y automatizacion de reportes\\datoschile.xlsx",
sheet = "Hoja1",
detectDates = T)`
magrupados3 <- melt(datos, id.vars = "Periodo")%>%
filter(variable == "Exportaciones" | variable == "Importaciones")`
Then with this data, I have created a graph using geom_line and geom_point and also the tendency line using geom_smooth. This is the code and the result you can see it here:
magrupados4 <- ggplot(data=magrupados3,aes(x=Periodo,y=value))+
geom_line()+geom_point()+facet_wrap(variable~.,scales = "free",ncol = 2)+
geom_smooth(method = "lm",formula= y~x,col="black",se=F)`
Graph
I want to make a transition of this graph. In other words, I want that geom_line and the regression line (geom_smooth) to appear simultaneously each year(my variable that represents year in my data is called "Periodo").
Can someone help me with this?
It seems that you need to use the R package gganimate in combination with the R packages: devtools, ggplot2, gifski, and av.
Check this link: https://gganimate.com/
Here is an example that I took from that website. It is working in my computer. You need the libraries that are in my example. Some libraries were not in the website example. Be careful with that. In your code, you did not use the last two lines of this example (transition_time(year) + ease_aes('linear')). Also, I guess that you did not install all the R libraries that are needed.
# install.packages('devtools')
# devtools::install_github('thomasp85/gganimate')
library(devtools)
library(gapminder)
library(ggplot2)
library(gganimate)
library(gifski)
library(av)
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) + labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy')+
transition_time(year) +
ease_aes('linear')
Related
To examine an interaction effect in my research I've created post hoc slopes using the function “emtrends” of the package emmeans (version 1.4.5.; Lenth, 2020) and visualized it in a plot using 'emmip'. I've used the code below:
emtrends(model_c, pairwise ~ Condition, var = "prsa_clean$meat_amount_c")
emmip(model_c, Condition ~ meat_amount_c, cov.reduce = range, ylab = "Favourability", xlab = "Meat consumption")
The resulting plot shows the 3 slope lines of my 3 conditions in blue/green/red lines, but doesn't adhere to the APA standards for figures, so I'm trying to adjust it using ggplot2. I manage to successfully edit the background and linesizes to my liking using the code below:
plot <- emmip(model_c, Condition ~ meat_amount_c, cov.reduce = range, ylab = "Favourability", xlab = "Meat consumption")
plot <-plot +theme_bw() + geom_line(size = 1.25)
However, I would like the lines to be different types. I've found the appropriate code to adjust this with ggplot is to use the function geom_line(linetype = ""). I've tried to add the code in the same way as the theme and linesize, but it doesn't work. Example tries of code below:
plot <- plot +theme_bw() + geom_line(size = 1.25) + geom_line(linetype = "dashed")
plot <- plot +theme_bw() + geom_line(size = 1.25) + geom_line(aes(linetype = Condition))
plot <- plot +theme_bw() + geom_line(size = 1.25) + scale_linetype_manual(values=c("twodash", "dotted", "solid"))
I'm not a regular R user so I'm probably missing something pretty obvious. I hope someone can help me out.
In sum, I expected the geom_line(linetype="") argument to change the existing lines in the plot to become the designated types, but they remained unchanged. The theme_() and geom_line(size=) worked similarly so I expected similar results.
Following my comment I tried some things with the emmip helpfile examples.
You can change line size within emmip with linearg = list(size = 1.25)
You can add scale_linetype_manual to the output
Example code:
library(emmeans)
noise.lm = lm(noise ~ size * type * side, data = auto.noise)
emmip(noise.lm, type ~ side * size, CIs = TRUE,
linearg = list(size = 1.25)) +
ggplot2::scale_linetype_manual(values=c("twodash", "dotted"))
This question has two parts, one more general and the other a specific case:
Is there a theme or template in R for producing plots that have similar appearance to the charts published in "The Economist" magazine? Examples in other contexts include: Create "The Economist" style graphs from python for python and set scheme economist for Stata.
Specifically, what would be the syntax (e.g., in ggplot2) to produce a groups bar plot that would look like the example below, colored shaped markers with bold lines spanning the range between them (left panel), or rectangular confidence intervals (right panel)?
Source: https://www.economist.com/graphic-detail/2020/04/01/covid-19-may-be-far-more-prevalent-than-previously-thought
Yes you have it in ggthemes (extension of ggplot2) with theme_economist and theme_economist_white.
For the bar plot, you will need to play with geom_bar and coord_flip (here)
Examples from ggthemes doc (here)
library("ggplot2")
library("ggthemes")
p <- ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg, colour = factor(gear))) +
facet_wrap(~am) +
# Economist puts x-axis labels on the right-hand side
scale_y_continuous(position = "right")
## Standard
p + theme_economist() +
scale_colour_economist()
## White
p + theme_economist_white() +
scale_colour_economist()
How to reproduce the plot given in example
Since I cannot install SciencesPo package in my computer, I propose you a ggplot + ggthemes approach.
A good starting point might be the following approach. I use as an example the diamond dataset.
library(dplyr)
library(ggplot2)
library(ggthemes)
df <- diamonds %>%
group_by(cut) %>%
summarise(mean = mean(price), sigma = sd(price),
n = n())
df <- df %>%
mutate(int_minus = mean - 1.96*sigma/sqrt(n),
int_plus = mean + 1.96*sigma/sqrt(n))
And then the plot
ggplot(df) +
geom_segment(aes(x = int_minus, xend = int_plus, y = factor(cut), yend = factor(cut)), size = 2L, alpha = 0.4) +
geom_point(aes(x = mean, y = factor(cut)), shape = 15, color = "blue", size = 4L) +
theme_economist_white()
I am trying to do a simple logged ggplot, showing the change in tree and shrub density over time (site age). the tree species are split into native / exotic.
I have also downloaded the viridis package, to enable a type of coloration to the legend+line+points+confidence interval fill.
The problem is, when I do plot using the viridis code, I get two separate legends, which I don't want. I can't figure out how to keep the viridis legend, and remove the other legend.
I would love to provide a picture of my output - but can't figure out how to add it to this question template...
this is the code I have used:
attach(data.df4)
base <- ggplot(data.df4, aes(age, total_trees))
base +
theme_classic(base_size = 10, base_family = "times") +
scale_y_log10() +
geom_point(aes(color = status)) +
geom_smooth(aes(color = status, fill = status), method = "lm", se = TRUE) +
scale_colour_viridis(discrete = TRUE, option = "D")+
scale_fill_viridis(discrete = TRUE, option = "D") +
labs(title = "changes in planted canopy and subcanopy tree and shrub density over time",
x = "planting age",
y = "density (plot-level)")
Without seeing your data or a screenshot, it's hard to know what needs to change. You can remove legends you don't want in 2 different ways
turn off the fill legend ggplot() + guides(fill = FALSE)
specify not to create a legend within the layer geom_smooth(..., show.legend = FALSE)
This article can show you how to post some sample data:
https://reprex.tidyverse.org/articles/articles/datapasta-reprex.html
I've made a violin plot that looks like this:
As we can see most of the data lies near the region where the score is 0.90-0.95. What I wish is to focus on the interval 0.75 to 1.00 by changing the scale giving less space to ratings from 0 to 0.75.
Is there a way to do this?
This is the code I'm currently using to create the violin plot:
ggplot(data=Violin_plots, aes(x = Year, y = Score)) +
geom_violin(aes(fill = Violin_plots$Year), trim = TRUE) +
coord_flip()+
scale_fill_brewer(palette = "Blues") +
theme(legend.position = 'none') +
labs(y = "Rating score",
fill = "Rating year",
title = "Violin-plots of credit rating scores")
While it's possible to transform the scale to focus more in the upper region (e.g. add trans = "exp" as an argument to the scale), a non linear scale is often hard to interpret appropriately.
For such use cases, I recommend facet_zoom from the ggforce package, which is pretty much built for this exact purpose (see vignette here).
I also switched from geom_violin() + coord_flip() to geom_violinh from the ggstance package, which extends ggplot2 by providing flipped versions of ggplot components. Example with simulated data below:
library(ggforce) # for facet_zoom
library(ggstance) # for flipped version of geom_violin
ggplot(df,
aes(x = rating, y = year, fill = year)) +
geom_violinh() + # no need to specify trim = TRUE as it's the default
scale_fill_brewer(palette = "Blues") +
theme(legend.position = 'none') +
facet_zoom(xlim = c(0.75, 0.98)) # specify zoom range here
Sample data that simulates the characteristics of the data in the question:
df <- diamonds[, c("color", "price")]
df$rating <- (max(df$price) - df$price) / max(df$price)
df$year <- df$color
You could create a second plot to zoom in on the original plot, without modifying the data, by using ggplot2::coord_cartesian()
ggplot(data=Violin_plots, aes(x=Year,y=Score*100)) +
geom_violin(aes(fill=Violin_plots$Year),trim=TRUE) +
coord_flip() +
coord_cartesian(xlim = c(0.75, 1.00)) +
scale_fill_brewer(palette="Blues") +
theme(legend.position='none') +
labs(y="Rating score",fill="Rating year",title="Violin-plots of credit rating scores")
Does anyone know how to create a scatterplot in R to create plots like these in PRISM's graphpad:
I tried using boxplots but they don't display the data the way I want it. These column scatterplots that graphpad can generate show the data better for me.
Any suggestions would be appreciated.
As #smillig mentioned, you can achieve this using ggplot2. The code below reproduces the plot that you are after pretty well - warning it is quite tricky. First load the ggplot2 package and generate some data:
library(ggplot2)
dd = data.frame(values=runif(21), type = c("Control", "Treated", "Treated + A"))
Next change the default theme:
theme_set(theme_bw())
Now we build the plot.
Construct a base object - nothing is plotted:
g = ggplot(dd, aes(type, values))
Add on the points: adjust the default jitter and change glyph according to type:
g = g + geom_jitter(aes(pch=type), position=position_jitter(width=0.1))
Add on the "box": calculate where the box ends. In this case, I've chosen the average value. If you don't want the box, just omit this step.
g = g + stat_summary(fun.y = function(i) mean(i),
geom="bar", fill="white", colour="black")
Add on some error bars: calculate the upper/lower bounds and adjust the bar width:
g = g + stat_summary(
fun.ymax=function(i) mean(i) + qt(0.975, length(i))*sd(i)/length(i),
fun.ymin=function(i) mean(i) - qt(0.975, length(i)) *sd(i)/length(i),
geom="errorbar", width=0.2)
Display the plot
g
In my R code above I used stat_summary to calculate the values needed on the fly. You could also create separate data frames and use geom_errorbar and geom_bar.
To use base R, have a look at my answer to this question.
If you don't mind using the ggplot2 package, there's an easy way to make similar graphics with geom_boxplot and geom_jitter. Using the mtcars example data:
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() + geom_jitter() + theme_bw()
which produces the following graphic:
The documentation can be seen here: http://had.co.nz/ggplot2/geom_boxplot.html
I recently faced the same problem and found my own solution, using ggplot2.
As an example, I created a subset of the chickwts dataset.
library(ggplot2)
library(dplyr)
data(chickwts)
Dataset <- chickwts %>%
filter(feed == "sunflower" | feed == "soybean")
Since in geom_dotplot() is not possible to change the dots to symbols, I used the geom_jitter() as follow:
Dataset %>%
ggplot(aes(feed, weight, fill = feed)) +
geom_jitter(aes(shape = feed, col = feed), size = 2.5, width = 0.1)+
stat_summary(fun = mean, geom = "crossbar", width = 0.7,
col = c("#9E0142","#3288BD")) +
scale_fill_manual(values = c("#9E0142","#3288BD")) +
scale_colour_manual(values = c("#9E0142","#3288BD")) +
theme_bw()
This is the final plot:
For more details, you can have a look at this post:
http://withheadintheclouds1.blogspot.com/2021/04/building-dot-plot-in-r-similar-to-those.html?m=1