I have two ggplot2 that I want to transform to ggplotly with the idea of interaction between both figures.
The problem is that the DF for the second needs a transformation. I know how to do it with plotly from scratch but I need do it from ggplot2.
Here is my code:
require(dplyr)
require(lubridate)
require(ggplot2)
require(gridExtra)
require(plotly)
My data:
df1<-tibble(date=seq.Date(as.Date("2000-01-01"),as.Date("2003-12-31"),by="1 month"),
value=sample(10:20,48,replace = TRUE))
df2<-df1 %>% mutate(year=year(date))
df3<-df2 %>%
group_by(year) %>%
summarise(max=max(value),mean=mean(value),min=min(value))
This is the final output with ggplot2 without interaction
graf1<-ggplot(df2)+geom_line(aes(date,value,color=factor(year),group=year),size=4)
graf2<-ggplot(df3)+
geom_segment(aes(x=year,xend=year,y=min,yend=max,color=factor(year),group=year),size=10)+
geom_point(aes(year,mean),size=2)
grid.arrange(graf1,graf2,nrow=2)
This is the my proposal to do it from ggplot2 to ggplotly (and doesn't work)
df2Linked<-highlight_key(df2,~year)
graf1<-ggplot(df2Linked)+geom_line(aes(date,value,color=factor(year),group=year),size=4)
graf1Ly<-ggplotly(graf1)%>% highlight(on = "plotly_hover", off = "plotly_deselect")
#it works!!
#**that NOT WORK in this way**, as its an object of class "c('SharedData', 'R6')"
df3<-df2Linked %>% group_by(year) %>% summarise(max=max(value),mean=mean(value),min=min(value))
graf2<-ggplot(df3)+geom_segment(aes(x=year,xend=year,y=min,yend=max,color=factor(year),group=year),size=10)+geom_point(aes(year,mean),size=2)
graf2Ly<-ggplotly(graf2)%>% highlight(on = "plotly_hover", off = "plotly_deselect")
subplot(graf1Ly,graf2Ly,nrows=2)
How has to be done it?
thanks
My suggestion is to use ggplotly() in each plot and than use subplot() from plotly
This isn't the pretty plot ever, but I think that it will help you:
graf1<-ggplot(df2)+
geom_line(aes(date,value,color=factor(year),group=year),size=4) +
labs(color='Year')
graf2<-ggplot(df3)+
geom_segment(aes(x=year,xend=year,y=min,yend=max,color=factor(year),group=year),size=10)+
geom_point(aes(year,mean),size=2) +
labs(color='Year')
subplot(ggplotly(graf1),ggplotly(graf2), nrows = 2)
The output:
Related
I would like to make four boxplots side-by-side using ggplot2, but I am struggling to find an explanation that suits my purposes.
I am using the well-known Iris dataset, and I simply want to make a chart that has boxplots of the values for sepal.length, sepal.width, petal.length, and petal.width all next to one another. These are all numerical values.
I feel like this should be really straightforward but I am struggling to figure this one out.
Any help would be appreciated.
Try this. The approach would be to selecting the numeric variables and with tidyverse functions reshape to long in order to sketch the desired plot. You can use facet_wrap() in order to create a matrix style plot or avoid it to have only one plot. Here the code (Two options):
library(tidyverse)
#Data
data("iris")
#Code
iris %>% select(-Species) %>%
pivot_longer(everything()) %>%
ggplot(aes(x=name,y=value,fill=name))+
geom_boxplot()+
facet_wrap(.~name,scale='free')
Output:
Or if you want all the data in one plot, you can avoid the facet_wrap() and use this:
#Code 2
iris %>% select(-Species) %>%
pivot_longer(everything()) %>%
ggplot(aes(x=name,y=value,fill=name))+
geom_boxplot()
Output:
This is a one-liner using reshape2::melt
ggplot(reshape2::melt(iris), aes(variable, value, fill = variable)) + geom_boxplot()
In base R, it can be done more easily in a one-liner
boxplot(iris[-5])
Or using ggboxplot from ggpubr
library(ggpubr)
library(dplyr)
library(tidyr)
iris %>%
select(-Species) %>%
pivot_longer(everything()) %>%
ggboxplot(x = 'name', fill = "name", y = 'value',
palette = c("#00AFBB", "#E7B800", "#FC4E07", "#00FABA"))
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 need to get a scatter plot like this based on 2 categorical variables where each variable has 2 levels.
I am using ggvis package in R.
This is my code so far
salab<- read.table("http://users.stat.ufl.edu
/~rrandles/sta4210/Rclassnotes/data/textdatasets/KutnerData/
Chapter%2022%20Data%20Sets/CH22TA06.txt", quote="\"", comment.char="")
salab %>% ggvis(~V2, ~V1, fill = ~factor(V3*V4)) %>% layer_points()
Which is incorrect because i need 4 factors combinations. Can anyone help me to figure out what modification should i do ?
I think you need factor(V3):factor(V4) instead of factor(V3*V4):
salab %>%
ggvis(~V2, ~V1, fill = ~ factor(V3):factor(V4)) %>%
layer_points()
An alternative:
salab$`V3*V4`<-paste0("V3=",salab$V3,"*","V4=",salab$V4)
salab %>% ggvis(~V2, ~V1, shape = ~`V3*V4`) %>% layer_points()
Is this possible. I want to do something like
library(plotly)
library(dplyr)
df <- data.frame(a=c(1,2,3,4,5,6),b=c(3,5,2,6,8,1),c=c("p","p","q","r","r","r"),
d=c("v","v","w","v","v","v"), stringsAsFactors= F)
df %>%
plot_ly(x=a,y=b,mode="markers",type="scatter", color=c,
marker = list(
symbol=d)
)
But the different symbol I want on third data point is not appearing
Also is it possible to set the symbols to be other than default
TIA
Just copying my comment here:
If you want to stick with plotly, you can use:
df %>% plot_ly(x=a,y=b,mode="markers", symbol=d, color=c)
You can also define your symbols with:
symbols = c("cross", "square", "triangle-down")
EDIT:
You could create an interaction and then use this new factor as a symbol. You will have only one legend (I don't think having two legends is possible with native plotly).
df$Int <- interaction(df$c, df$d)
df %>% plot_ly(x=a,y=b,mode="markers", symbol=Int)
But you could also use ggplot2 using:
ggplot(data=df, aes(x=a, y=b, shape=d, color=c)) + geom_point()
ggplotly()
Trying to replicate the ggplot function position="fill" in ggvis. I use this handy function all the time in the presentation of results. Reproducible example successfully performed in ggplot2 + the ggvis code. Can it be done using the scale_numeric function?
library(ggplot2)
p <- ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs)))
p+geom_bar()
p+geom_bar(position="fill")
library(ggvis)
q <- mtcars %>%
ggvis(~factor(cyl), fill = ~factor(vs))%>%
layer_bars()
# Something like this?
q %>% scale_numeric("y", domain = c(0,1))
I think that to do this sort of thing with ggvis you have to do the heavy data reshaping lifting before sending it to ggvis. ggplot2's geom_bar handily does a lot of calculations (counting things up, weighting them, etc) for you that you need to do explicitly yourself in ggvis. So try something like the below (there may be more elegant ways):
mtcars %>%
mutate(cyl=factor(cyl), vs=as.factor(vs)) %>%
group_by(cyl, vs) %>%
summarise(count=length(mpg)) %>%
group_by(cyl) %>%
mutate(proportion = count / sum(count)) %>%
ggvis(x= ~cyl, y = ~proportion, fill = ~vs) %>%
layer_bars()