I'm trying to plot this graph using R but dont know how.
Screenshot of the dataset that I'm using
You didn't provide the dataset, but you could do something like that
# packages ----------------------------------------------------------------
library(dplyr)
library(ggplot2)
# data --------------------------------------------------------------------
#load your data first
#
#
#get just the last observation for each location
dataset2 <- dataset %>%
group_by(location) %>%
slice_tail() %>%
ungroup()
# plot --------------------------------------------------------------------
dataset2 %>%
ggplot(aes(x = continent, y = total_deaths))+
geom_bar()
Related
Data
Here is the simulated data for my question. It consists of subjects, items (stimuli), and a T/F response to each item:
#### Load Tidyverse ####
library(tidyverse)
library(gganimate)
#### Create Tibble ####
set.seed(123)
subject <- factor(rep(1:5,100))
score <- rbinom(n=500,
size=1,
prob=.5)
tib <- tibble(subject,
score) %>%
group_by(subject) %>%
mutate(item = row_number())
tib
Problem
I'm trying to figure out how to animate either a single subject or many subject responses over time. If I plot the change over time in this way:
#### Plot Change Over Items ####
tib %>%
ggplot(aes(x=item,
y=score,
color=subject))+
geom_point()+
geom_smooth(se=F)
I can at least see generally speaking where the trends lie. However, I would like to have something animated which shows the progression of responses as they happen. I tried using gganimate, but it wouldn't use geom_smooth and the points alone are lacking a lot of useful information:
#### Plot Change Over Items ####
tib %>%
ggplot(aes(x=item,
y=score,
color=subject))+
geom_point()+
transition_manual(item)
I tried a cumulative sum plot as well:
#### Plot Cumulative Sum ####
tib %>%
mutate(cum_score = cumsum(score)) %>%
ggplot(aes(x=item,
y=cum_score,
color=subject))+
geom_line()
But animating it still comes out poor:
#### Plot Cumulative Sum ####
tib %>%
mutate(cum_score = cumsum(score)) %>%
ggplot(aes(x=item,
y=cum_score,
color=subject))+
geom_line()+
transition_manual(cum_score)
Am I messing up the arguments here? Is there a better alternative?
I figured it out. I was trying to figure out how to use the cumulative argument and I realized it was a logical argument:
#### Plot Cumulative Sum ####
tib %>%
mutate(cum_score = cumsum(score)) %>%
ggplot(aes(x=item,
y=cum_score,
color=subject))+
geom_line()+
geom_point()+
transition_manual(cumulative = T,
frames = cum_score)
Which gives me a nice gif:
I want to use to following exampe to do t-tests with multiple variables - The code is used from https://www.datanovia.com/en/blog/how-to-perform-multiple-t-test-in-r-for-different-variables/:
options(scipen = 99)
# Load required R packages
library(tidyverse)
library(rstatix)
library(ggpubr)
# Prepare the data and inspect a random sample of the data
mydata <- iris %>%
filter(Species != "setosa") %>%
as_tibble()
mydata %>% sample_n(6)
# Transform the data into long format
# Put all variables in the same column except `Species`, the grouping variable
mydata.long <- mydata %>%
pivot_longer(-Species, names_to = "variables", values_to = "value")
mydata.long %>% sample_n(6)
stat.test <- mydata.long %>%
group_by(variables) %>%
t_test(value ~ Species) %>%
adjust_pvalue(method = "BH") %>%
add_significance()
stat.test
This tutorial uses the t_test function of the rstatix package. It works great, but is there a way to disable the scientific notation of the p-values? I want to output p-values like 0.000445 instead of 4.45e-4.
Unfortunetely the use of
options(scipen = 99)
did not change anything.
Thank you!
EDIT: The solution can be found in the comments - it is necessary to call stat.test this way:
as.data.frame(stat.test)
Thank rawr for his comment!
I want to create a line chart with two lines in one plot using ggplot. However, one line chart has missing values in-between:
year<-c(1990,1991,1992,1993)
v1<-c(1,NA,NA,2)
v2<-c(2,3,1,2)
I want the second line (v2) to connect its first value in 1990 with its last one in 1993. Is this possible using ggplot?
Try this approach reshaping your data:
library(ggplot2)
library(dplyr)
library(tidyr)
#Data
year<-c(1990,1991,1992,1993)
v1<-c(1,NA,NA,2)
v2<-c(2,3,1,2)
df <- data.frame(year,v1,v2)
#Plot
df %>% pivot_longer(-year) %>%
filter(!is.na(value)) %>%
ggplot(aes(x=factor(year),y=value,color=name,group=name))+
geom_point()+
geom_line()+xlab('year')+
labs(color='Var')
Output:
I've got a dataset of species occurrences which I'm trying to convert into areas of occurrence by making convex hulls. I'm able to do this manually (ie. one species at a time) but I'd really love to be able to just have it handled automatically by the species name.
A pared-down example dataset can be found here: https://pastebin.com/dWxEvyUB
Here's how I'm currently doing it manually:
library(tidyverse)
library(sf)
library(rgeos)
library(maps)
library(mapview)
library(mapdata)
library(ggplot2)
fd <- read_csv("occurrence.csv")
spA.dist <- fd %>%
filter(species == "sp.A") %>%
dplyr::select(lon,lat) %>%
as.matrix() %>%
coords2Polygons(ID="distribution") %>%
gConvexHull() %>%
gBuffer()
spB.dist <- fd %>%
filter(species == "sp.B") %>%
dplyr::select(lon,lat) %>%
as.matrix() %>%
coords2Polygons(ID="distribution") %>%
gConvexHull() %>%
gBuffer()
wrld2 = st_as_sf(map('world2', plot=F, fill=T))
ggplot() +
geom_sf(data=wrld2, fill='gray20',color="lightgrey",size=0.07) +
geom_polygon(aes(x=long,y=lat,group=group),color="red",data=spA.dist,fill=NA) +
geom_polygon(aes(x=long,y=lat,group=group),color="blue",data=spB.dist,fill=NA) +
coord_sf(xlim=c(100,300), ylim=c(-60,60))
That displays a map with the two species occurrence areas based on the convex hull of their observations. I realize I'm mixing different spatial libraries here so it would be nice to do it all in sf if possible. In my real data I have more than two species and I can copy and paste the code I've got for each one but it seems like it should be possible to simplify this so the polygons (and subsequent convex hulls) are constructed by factor level automatically. Something more like this:
polys <- st_as_sf(fd) %>%
group_by(species) %>%
magically_make_polygons(lon,lat) %>%
st_convex_hull() %>%
st_buffer()
I've been searching for days as well as digging through reams of documentation. A lot of this spatial stuff is non-intuitive to me so I expect there's a lot of basic understanding I'm missing. Can this be done?
Here is a possible solution using the tidyverse (in fact only dplyr) and the sf-package (and the mapview package for some quick viewing).
You were very close with your own solution (kudo's). The trick is to summarise the grouped data, and then create the hulls..
library( tidyverse )
library( sf )
#create simple feature
df.sf <- df %>%
st_as_sf( coords = c( "lon", "lat" ), crs = 4326 )
#what are we working with?
# perform fast visual check using mapview-package
mapview::mapview( df.sf )
#group and summarise by species, and draw hulls
hulls <- df.sf %>%
group_by( species ) %>%
summarise( geometry = st_combine( geometry ) ) %>%
st_convex_hull()
#result
mapview::mapview( list( df.sf, hulls ) )
I want to create a plot in R with ggplot() to visualise the data included in variable matrix that looks like this:
matrix <- matrix(c(time =c(1,2,3,4,5),v1=rnorm(5),v2=c(NA,1,0.5,0,0.1)),nrow=5)
colnames(matrix) <- c("time","v1","v2")
df <-data.frame(
time=rep(matrix[,1],2),
values=c(matrix[,2],matrix[,3]),
names=rep(c("v1","v2"), each=length(matrix[,1]))
)
ggplot(df, aes(x=time,y=values,color=names)) +
geom_point()+
facet_grid(names~.)
Is there a faster way than transforming the data in a data.frame like I do? This way seems to be very laborious..
I would appreciate every help!! Thanks in advance.
A tidyverse approach:
This will produce the data structure you need to use in ggplot
library(tidyverse)
matrix %>%
as_data_frame() %>%
gather(., names, value, -time)
This will generate data structure and plot all at once
matrix %>%
as_data_frame() %>%
gather(., names, value, -time) %>%
ggplot(., aes(x=time,y=value,color=names)) +
geom_point()+
facet_grid(names~.)