I am trying to come up with a way to plot the following graph in R. I have looked up a lot but did not find a solution.
This is an example of how data are:
df<-data.frame(matrix(NA,nrow=20,ncol=3)) colnames(df)<-c("time","A","B") df$time<-1:20 df$A<-c(rep("Success",3),rep("Fail",5),rep("Success",9),rep("Fail",3)) df$B<-c(rep("Success",7),rep("Fail",2),rep("Success",4),rep("Fail",7))
Any help to create the plot in R would be greatly appreciated.
This approach can be useful:
library(dplyr)
library(ggplot2)
#Code
df %>% pivot_longer(-time) %>%
ggplot(aes(x=time,y=name,fill=value))+
geom_tile()
Output:
Related
I have a problem making a graph in R. I have the following data, with flower type, and a color index for different points (distance_petal).
flower_type<-c(rep("blue",3),rep("red",3))
distance_petal1<-c(2,3,2,7,6,7)
distance_petal2<-c(2,2,1,8,6,8)
distance_petal3<-c(1,1,2,9,9,8)
data<-as.data.frame(cbind(flower_type,distance_petal1,distance_petal2,distance_petal3))
data$flower_type<-as.factor(flower_type)
I am trying to make a graph showing distance_petal1, distance_petal2 and distance_petal3 in the X-axis, and the value in the Y-axis. So I want to obtain 2 lines, one for each flower type with 3 points in the Y-axis.
I mean, I want to make something like this, but instead of plotting all values, just plotting the mean for each variable for each factor.
library(GGally)
ggparcoord(data,
columns = 2:4, groupColumn = 1,scale="globalminmax"
)
Does anyone know how to do this?
Thank you very much in advance, have a nice day!
Using ggplot, dplyr and tidyr you can try:
Not sure if your question asks for a line as in the example from GGally or if you want points as in the question. So have included both the line and point version you can just remove the line of ggplot code to get what you need.
library(dplyr)
library(tidyr)
library(ggplot2)
data %>%
pivot_longer(-1) %>%
mutate(value = as.numeric(value))%>%
group_by(flower_type, name) %>%
summarise(mean = mean(value)) %>%
ggplot(aes(name, mean, colour = flower_type))+
geom_line(aes(group = flower_type))+
geom_point()
Created on 2021-04-25 by the reprex package (v2.0.0)
Hello,
I have a dateset structured as shown in the link above. I am extremely new to R. And this is probably super easy to get done. But I cannot figure out how to plot this dataset using ggplot...
Could anyone guide and give me hints?
I basically want to color lines according to socioeconomic levels and visualize it by each years' value...
You need to reshape you data to run ggplot.
library(reshape)
library(dplyr)
library(ggplot2)
df_long <- melt(df) # reshape the dataframe to a long format
df_long %>%
ggplot( aes(x=variable, y=value, group=group, color=group)) +
geom_line()
Note: You will get better answers if you post your code with a reproducible dataset.
I am trying to sort y-axis numerically according to population values. Have tried other stackoverflow answers that suggested reorder/ converting columns to numeric data type (as.numeric), but those solutions does not seem to work for me.
Without using reorder, the plot is sorted alphabetically:
Using reorder, the plot is sorted as such:
The code i am using:
library(ggplot2)
library(ggpubr)
library(readr)
library(tidyverse)
library(lemon)
library(dplyr)
pop_data <- read_csv("respopagesextod2011to2020.csv")
temp2 <- pop_data %>% filter(`Time` == '2019')
ggplot(data=temp2,aes(x=reorder(PA, Pop),y=Pop)) + geom_bar(stat='identity') + coord_flip()
How should I go about sorting my y-axis? Any help will be much appreciated. Thanks!
I am using data filtered from: https://www.singstat.gov.sg/-/media/files/find_data/population/statistical_tables/singapore-residents-by-planning-areasubzone-age-group-sex-and-type-of-dwelling-june-20112020.zip
The functions are all working as intended - the reason you don't see the result as expected is because the reorder() function is specifying the ordering of the pop_data$PA based on each observation in the set, whereas the bars you are plotting are a result of summary statistics on pop_data.
The easiest solution is to probably perform the summarizing first, then plot and reorder the summarized dataset. This way, the reordering reflects an ordering of the summarized data, which is what you want.
temp3 <- pop_data %>% filter(`Time` == '2019') %>%
group_by(PA) %>%
summarize(Pop = sum(Pop))
ggplot(data=temp3, aes(x=reorder(PA, Pop),y=Pop)) +
geom_bar(stat='identity') + coord_flip()
i am trying to add wilcoxon stats in my graph, but the "stat_compare_means" does not work...
i have tried both ggplot and ggplot2.
library(readxl)
library(dplyr)
library(tidyverse)
library(ggpubr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(Rtsne)
require(ggpubr)
#excel sheet resolution, voxel size comparison
data<-read_excel("res_all.xlsx", sheet="resolution")
# transform to long format using dplyr (included in tidyverse)
data_long <- as_tibble(data) %>%
gather(key, value,-parameter) %>%
mutate(cohort=ifelse(grepl("per",key), "per", "val"))
# plot graph
graph <- ggplot(data_long) +
aes(x=parameter, y=value, fill=cohort)+
geom_boxplot()+
stat_compare_means(method= "wilcox.test")
graph + ggtitle("Resolution comparison")+
theme_minimal()
error is Error in stat_compare_means(method = "wilcox.test") :
could not find function "stat_compare_means"
is it any other way to add W and p-values in my graph?
Thank you in advance.
[1]: https://i.stack.imgur.com/yfp8E.png
I think you forgot a "+" after theme_minimal().
Oh, and stat_compare_means is from ggpubr package, not ggplot. be sure you included it. Check if you have library(ggpubr) or require(ggpubr) in your R session. It is good if you can include full code and result in sessioninfo() for further troubleshoot.
The stat_compare_means() was introduced in ggpubr ver 0,1,3. So check the package with ?ggpubr for the version and lsf.str("package:ggpubr") to list all functions inside the package.
I am trying to create a clustered bar plot for 3 different types of precipitation data. I've been doing various searches, how this might be done in R with a similar data set. However, I couldn't find any good help.
This is the dataset I am currently using. I have tried adding multiple geom_bar() but that didn't work out. See attempt below:
ggplot(ppSAcc,aes(x=date,y=as.numeric(Precipitation)))+geom_bar(stat="identity",aes(color="blue"),show.legend=FALSE,size=1)+
geom_bar(ppMAcc,stat="identity",aes(x=date,y=as.numeric(Precipitation),color="purple"),show.legend = FALSE,size=1)+
labs(title="Accumulated Solid Precipitation (Snow)",y="Precipitation (mm)")
In my second attempt, I tried creating a dataframe which includes all three precipitation types.
data<-data.frame(date=ppSAcc$date,snow=ppSAcc$Precipitation,mixed=ppMAcc$Precipitation,rain=ppRAcc$Precipitation)
Which gave me the dataframe shown above.
This is where I am stuck. I started coding ggplot ggplot(data,aes(x=date)))+geom_bar(position = "dodge",stat = "identity") but I'm not sure how to write the code such that I will have three columns(snow, mixed, rain) for each year. I'm not sure how to set the aes() part.
You need to reshape your dataframe into a longer format before to plot it in ggplot2. You can use pivot_longer function from tidyr:
library(tidyr)
library(dplyr)
library(ggplot2)
library(lubridate)
df %>% pivot_longer(-date, names_to = "var", values_to = "val") %>%
ggplot(aes(x = ymd(date), y= val, fill = var))+
geom_col(position = position_dodge())
Does it answer your question ?
If not, please provide a reproducible example of your dataset by following this guide: How to make a great R reproducible example