I have many individual ggplotly objects, some of them with legends and some without.
I want to create a single view of shared X axis for these ggplotly objects.(i am using subplot() of plotly for this )
With the subplots approach I want to achieve the following
Not group all legends into one legend.
Dont add legends when the individual ggplotly object doesnt have one.
Sample Code
df <- structure(list(Tool = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L), .Label = c("ToolA", "ToolB", "ToolC"), class = "factor"),
StartTime = structure(c(1456383600, 1464291720, 1456383600,
1460710380, 1464291780, 1456383600, 1456383600, 1460710380
), class = c("POSIXct", "POSIXt"), tzone = "MET"), Category = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 4L, 4L), .Label = c("C1", "C2", "C3",
"null"), class = "factor"), Type = structure(c(4L, 2L, 2L,
3L, 3L, 2L, 1L, 1L), .Label = c("null", "T1", "T2", "T3"), class = "factor")), .Names = c("Tool",
"StartTime", "Category", "Type"), row.names = c(NA, -8L), class = "data.frame")
Data
> df
Tool StartTime Category Type
1 ToolA 2016-02-25 08:00:00 C1 T3
2 ToolA 2016-05-26 21:42:00 C2 T1
3 ToolA 2016-02-25 08:00:00 C3 T1
4 ToolB 2016-04-15 10:53:00 C1 T2
5 ToolB 2016-05-26 21:43:00 C2 T2
6 ToolB 2016-02-25 08:00:00 C3 T1
7 ToolC 2016-02-25 08:00:00 null null
8 ToolC 2016-04-15 10:53:00 null null
Plotly
pOne <- ggplotly(ggplot(data=df[df$Tool=="ToolA",],aes(x=StartTime ,y= Tool, colour=Type))+
geom_point(alpha=0.8,size=4)) %>% layout(legend = list(orientation = 'h'))
pTwo <- ggplotly(ggplot(data=df[df$Tool=="ToolB",],aes(x=StartTime ,y= Tool,colour=Category))+
geom_point(alpha=0.8,size=4)) %>% layout(legend = list(orientation = 'h'))
pThree <- ggplotly(ggplot(data=df[df$Tool=="ToolC",],aes(x=StartTime ,y= Tool))+
geom_point(alpha=0.8,size=4)) %>% layout(showlegend = FALSE)
subplot(pOne,pTwo,pThree, nrows=3, shareX= T,which_layout = 1)
Actual Behaviour
HIghlighted the legend in red
Expected Behaviour
How can I get the expected behaviour? Please help.
[Answering my own question, as this is the direction I took]
Instead of using plotly's subplot function, I found shiny dashboard to be a better solution. It can directly take multiple plots not just from plotly but also from ggplot objects.
Only disadvantage is that we cannot make the plots share the same axis in shiny dashboard. Apart from this I found that shinyDashboard also helps in better structuring the UI of the shiny app.
I fiddled around flexDashboard but din't find it interactive enough as shiny dashboard for my needs.
Related
Here is the dataframe df on which I'm trying to do a pivot using cast function
dput(df)
structure(list(Val = c(1L, 2L, 2L, 5L, 2L, 5L), `Perm 1` = structure(c(1L,
2L, 3L, 3L, 3L, 3L), .Label = c("Blue", "green", "yellow"
), class = "factor"), `Perm 2` = structure(c(1L, 2L, 2L, 3L,
3L, 3L), .Label = c("Blue", "green", "yellow"), class = "factor"),
`Perm 3` = structure(c(1L, 2L, 2L, 2L, 3L, 3L), .Label = c("Blue",
"green", "yellow"), class = "factor")), .Names = c("Val",
"Perm 1", "Perm 2", "Perm 3"), row.names = c(NA, 6L), class = "data.frame")
And expecting the data after pivot
Blue 1 1 1
green 2 4 9
yellow 14 12 7
I tried doing
cast(df, df$Val ~ df$`Perm 1`+df$`Perm 2`+df$`Perm 3`, sum, value = 'Val')
But this gives error
Error: Casting formula contains variables not found in molten data: df$Val, df$`Perm1`, df$`Perm2`
How can I be able to do pivot so that I'll be able to get the desired O/P
P.S- The dataframe DF has around 36 column but for simplicity I took only 3 columns.
Any suggestion will be appreciated.
Thank you
Domnick
It appears you want to sum, grouped by each permutation in your dataset. Although hacky, I think this works for your problem. First we create a function to perform that summation using tidyeval syntax. Link for more information: Group by multiple columns in dplyr, using string vector input
sum_f <- function(col, df) {
library(tidyverse)
df <- df %>%
group_by_at(col) %>%
summarise(Val = sum(Val)) %>%
ungroup()
df[,2]
}
We then apply it to your dataset using lapply, and binding together the summations.
bind_cols(lapply(c('Perm1', 'Perm2', 'Perm3'), sum_f, df))
This gets us the above answer.
Caveats: Need to know the name of the columns you have to sum over for this to work. Also, each column needs to have the same levels of your permutations i.e. blue, green, yellow. The code will respect this ordering.
I am trying to do a phylogenetic comparison of two trees which contain the same taxa. I want to colour the connections based on isolation site. I had thought I had performed this successfully but there is error in my work flow i.e. the coloured lines are not corresponding to isolation site accurately . I was wondering if you have any insights, please find my reproducible example below.
site <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L,9L, 10L, 2L), .Label = c("t1", "t10", "t2", "t3", "t4", "t5","t6", "t7", "t8", "t9"), class = "factor"), site = c(1L, 1L,1L, 2L, 2L, 3L, 1L, 3L, 2L, 2L)), .Names = c("name", "site"), row.names = c(NA,10L), class = "data.frame")
library(ape)
t1 <- rtree(10)
t2 <- rtree(10)
order <- cbind(t1$tip.label)
list <- merge(order, site, by.x="V1", by.y="name")
x <- list$site
A <- cbind(t1$tip.label, t1$tip.label)
cophyloplot(t1, t2, assoc = A, show.tip.label = T, space=50, col = x)
As it stands this is my current output:
Just spotted this thread on extracting tip labels and it works.
correct order of tip labels in ape
I also need to incorporate sort=F into the merge function.
So for a finish the workflow looks like:
site <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L,9L,
10L, 2L), .Label = c("t1", "t10", "t2", "t3", "t4", "t5","t6", "t7", "t8",
"t9"), class = "factor"), site = c(1L, 1L,1L, 2L, 2L, 3L, 1L, 3L, 2L, 2L)),
.Names = c("name", "site"), row.names = c(NA,10L), class = "data.frame")
library(ape)
t1 <- rtree(10)
t2 <- rtree(10)
is_tip<- t1$edge[,2] <= length(t1$tip.label)
ordered_tips <- t1$edge[is_tip,2]
order <-t1$tip.label[ordered_tips]
order <- as.data.frame(order)
list <- merge(order, site, by.x="V1", by.y="name", sort=F)
x <- list$site
A <- cbind(t1$tip.label, t1$tip.label)
cophyloplot(t1, t2, assoc = A, show.tip.label = T, space=50, col = x)
Only as a follow up, in my work the correct order of the labels was being altered by the merge command. My tree structure is quite complicated and probably the absence/presence of individuals between both trees was creating this problem. I just fixed by adding a vector with the positions to the order data.frame.
order <- as.data.frame(order, seq=seq(1:length(order)) )
Latter one can easily rearrange the data.frame accordingly with the tree structure.
Cheers,
This is what my dataframe looks like:
Persnr Date AmountHolidays
1 55312 X201101 2
2 55312 X201102 4.5
3 55312 X201103 5
etc.
What I want to have is a graph that shows the amount of holidays (on the y-axis) of each period (Date on the x-axis) of a specific person (persnr). Basically, it's a pivot graph in R. So far I know, it is not possible to create such a graph.
Something like this is my desired result:
http://imgur.com/62VsYdJ
Is it possible in the first place to create such a model in R? If not, what is the best way for me to visualise such graph in R?
Thanks in advance.
Something like this could do the trick?
dat <- read.table(text="Persnr Date AmountHolidays
55312 2011-01-01 2
55312 2011-02-01 4.5
55312 2011-03-01 5
55313 2011-01-01 4
55313 2011-02-01 2.5
55313 2011-03-01 6", header=TRUE)
dat$Date <- as.POSIXct(dat$Date)
dat$Persnr <- as.factor(dat$Persnr)
# Build a primary graph
plot(AmountHolidays ~ Date, data = dat[dat$Persnr==55312,], type="l", col="red",
xlim = c(1293858000, 1299301200), ylim=c(0,8))
# Add additional lines to it
lines(AmountHolidays ~ Date, data = dat[dat$Persnr==55313,], type="l", col="blue")
# Build and place a legend
legend(x=as.POSIXct("2011-02-19"), y=2.2, legend = levels(dat$Persnr),fill = c("red", "blue"))
To set X coordinates, you can either use as.POSIXct(YYYY-MM-DD) or as.numeric(as.POSIXct(YYYY-MM-DD) as I did for the xlim's.
You can try with package ggplot2:
First option
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line(aes(colour=Persnr)) + scale_colour_discrete()
or
Second option
ggplot(dat, aes(x=Date, y=AmountHolidays, group=Persnr)) +
geom_line() + facet_grid(~Persnr)
One of the advantages is that you don't need to have a line per Persnr or even to specify (to know) the name or number of Persnr.
example:
first option
second option
Data:
dat <- structure(list(Persnr = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("54000",
"55312"), class = "factor"), Date = structure(c(1L, 2L, 3L, 1L,
2L, 3L), .Label = c("2011-01-01", "2011-02-01", "2011-03-01"), class = "factor"),
AmountHolidays = c(5, 4.5, 2, 3, 6, 7)), .Names = c("Persnr",
"Date", "AmountHolidays"), row.names = c(3L, 5L, 6L, 1L, 2L,
4L), class = "data.frame")
Suppose I have this dataframe
latitude longitude category
42.39905 -72.93871 A
42.39905 -73.93871 B
43.37471 -73.36336 A
43.37471 -74.36336 B
44.28322 -74.31423 B
What I would like to do is to group the coordinates by its integer. Then for each group, I could create a bubble with a size function on the counts in a group.
The colour diverges from A to B, based on how many A than B. So far, I've been doing this,
map = get_map(location="jk",zoom=6,source="stamen")
#Plot the point
ggmap(map)+
geom_point(data=zipmap,
aes(x=round(longitude),y=round(latitude),colour=category))+
scale_color_brewer(type='div')
But as you would expect, the colour is not diverging, and the size of the bubble is not implemented. How could I achieve this? I can't use scale_x_continuous, as it already used somewhere in ggmap
Here is one direction to try.
dput(df)
structure(list(latitude = c(42.39905, 42.39905, 43.37471, 43.37471,
44.28322), longitude = c(-73, -74, -73, -74, -74), category = structure(c(1L,
2L, 1L, 2L, 2L), .Label = c("A", "B"), class = "factor"), latround = structure(c(1L,
1L, 2L, 2L, 3L), .Label = c("42", "43", "44"), class = "factor"),
longround = structure(c(2L, 1L, 2L, 1L, 1L), .Label = c("-74",
"-73"), class = "factor")), .Names = c("latitude", "longitude",
"category", "latround", "longround"), row.names = c(NA, -5L), class = "data.frame")
df$latround <- as.factor(round(df$latitude)) # round the coords
df$longround <- as.factor(round(df$longitude))
library(dplyr) # group by rounded coordinates and count the categories
df2 <- df %>% group_by(latround) %>% summarise(catnumber = n())
latround catnumber
1 42 2
2 43 2
3 44 1
library(ggmap)
From here you don't specify the location jk so I outlined an approach to plotting.
map <- get_map(location="jk",zoom=6,source="stamen")
#Plot the point
ggmap(map)+
geom_point(df2, aes(x=longround),y=latround), size = catnumber, colour=catnumber))+
scale_color_brewer(type='div') # more is needed in the ggmap code
I am interested in knowing if there is a lattice alternative to geom_tile() in ggplot2 when I want to display factor levels/map fill to text. Example data frame (df) follows...
Gene Sample Mutation
A1 2 Missense
A2 2 WT
A1 3 Missense
A2 3 Missense
With ggplot2 this is trivial
qplot(df, y=Gene, x=Sample, fill=Mutation, geom='tile')
what would the lattice equivalent of this be? (I am interested in this because axis alignment in ggplot2 between plots is convoluted and cumbersome currently).
df <- structure(list(Gene = structure(c(1L, 2L, 1L, 2L), .Label = c("A1", "A2"), class = "factor"),
Sample = structure(c(1L, 1L, 2L, 2L ), .Label = c("2", "3"), class = "factor"),
Mutation = structure(c(1L, 2L, 1L, 1L), .Label = c("Missense", "WT"), class = "factor")), .Names = c("Gene", "Sample", "Mutation"), row.names = c(NA, -4L), class = "data.frame")
Check out the levelplot() function in lattice, for example
library("lattice")
df <- transform(df, Sample = factor(Sample))
levelplot(Mutation ~ Gene * Sample, data = df)
You'll need to work out the colour scale key yourself though.