Position_fill function equivalent in ggvis? - r

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

Related

How to improve this graph with multiple lines in ggplo2?

Example dataframe: datafame.RData
I would like to create a chart below with these automated interactions. Ex. Changing the variable the average calculations are automatically remade and changed in the graph. For example Pais 'n' presents NA.
Here is an example of the expected chat in the output of ggplo2.
What I managed to do in R was this:
mydata %>%
dplyr::filter(Region %in% 'World median') %>%
dplyr::select(year,value) %>%
ggplot() +
aes(year,value, group=1,color="World median")+
geom_line()+
geom_line(data=mydata %>%
dplyr::filter(Country %in% 'Canada') %>%
dplyr::select(year,value),
aes(year, value, group=1, color="Canada"))+
geom_line(data=mydata %>%
dplyr::filter(Country %in% 'Brazil') %>%
dplyr::select(year,value),
aes(year, value, group=1, color="Brazil"))
The result was the one below. But if you have any suggestions on how to do better using ggplot I appreciate it.

Boxplots of four variables in the same plot

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

two ggplot2 previous to transform to plotly to obtain interactive

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:

How to create ggplot with facet_grid() for each column of a matrix

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

pipe (%>%) ggplot2 like ggvis

When I integrate tables and figures in a document using knitr, adding the code makes it more reproducible and interesting.
Often a combination of dplyr and ggvis can make a plot that has relatively legible code (using the magrittr pipe operator %>).
mtcars %>%
group_by(cyl, am) %>%
summarise( weight = mean(wt) ) %>%
ggvis(x=~am, y=~weight, fill=~cyl) %>%
layer_bars()
The problem is that the ggvis plot:
does not look quite as as pretty as the ggplot2 plot (I know, factoring of cyl):
However, for ggplot2 we need:
mtcars %>%
group_by(am, cyl) %>%
summarise( weight = mean(wt) ) %>%
ggplot( aes(x=am, y=weight, fill=cyl) ) +
geom_bar(stat='identity')
My problem is that this switches from %>% to + for piping. I know this is a very minor itch, but I would much prefer to use:
mtcars %>%
group_by(am, cyl) %>%
summarise( weight = mean(wt) ) %>%
ggplot( aes(x=am, y=weight, fill=cyl) ) %>%
geom_bar(stat='identity')
Is there a way to modify the behaviour of ggplot2 so that this would work?
ps. I don't like the idea of using magrittr's add() since this again make the code more complicated to read.
Since it would be too long to expand in the comments, and based on your answer I am not sure if you tried the bit of code I provided and it didn't work or you tried previously and didn't manage
geom_barw<-function(DF,x,y,fill,stat){
require(ggplot2)
p<-ggplot(DF,aes_string(x=x,y=y,fill=fill)) + geom_bar(stat=stat)
return(p)
}
library(magrittr)
library(dplyr)
library(ggplot2)
mtcars %>%
group_by(cyl, am) %>%
summarise( weight = mean(wt) ) %>%
geom_barw(x='am', y='weight', fill='cyl', stat='identity')
This works for me with:
dplyr_0.4.2 ggplot2_2.1.0 magrittr_1.5
Of course geom_barw could be modified so you don't need to use the quotes anymore.
EDIT: There should be more elegant and safer way with lazy (see the lazyeval package), but a very quick adaptation would be to use substitute (as pointed by Axeman - however without the deparse part):
geom_barw<-function(DF,x,y,fill,stat){
require(ggplot2)
x<-substitute(x)
y<-substitute(y)
fill<-substitute(fill)
p<- ggplot(DF,aes_string(x=x,y=y,fill=fill))
p<- p + geom_bar(stat=stat)
return(p)
}

Resources