I want to create a basic filled area plot with plotly using this dataset:
week<-c(2,1,3)
pts<-c(10,20,30)
wex<-data.frame(week,pts)
The x-axis should contain the week which as you can see may not be in order in the dataset but MUST be in order in the x-axis of the plot. The y axis should contain the pts.
For some reason I take nothing as a result but no error seems to exist.
library(plotly)
week<-c(2,1,3)
pts<-c(10,20,30)
wex<-data.frame(week,pts)
wex$week <- factor(wex$week, levels = wex[["week"]])
p <- plot_ly(x = ~wex$week, y = ~wex$pts,
type = 'scatter', mode = 'lines',fill = 'tozeroy')
p
Values on x axis need to be numeric (see example) and ordered.
wex <- wex[order(wex$week), ]
# wex$week <- factor(wex$week, levels = wex[["week"]])
plot_ly(x = ~wex$week, y = ~wex$pts, type = 'scatter', mode = 'lines',
fill = 'tozeroy')
This will work if you want to keep the x-axis, but it's not a pure plot_ly answer:
p <- ggplot(wex, aes(x = week, y = pts)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin = 0, ymax = pts), fill = "blue", alpha = .6, group = 1)
g <- ggplotly(p)
g
Related
I want to create a plot that adding vertical lines between two sets of points using plotly in R. I know how to do this in ggplot2, and I know I can use the ggplotly function to convert the ggplot2 object to a plotly object. However, I would like to learn how do this directly using plotly.
Below is an example code creating an example data frame and use ggplot2 and ggplotly to create the plot I want.
# Load packages
library(tidyverse)
library(plotly)
# Create example dataset
dat <- tibble(
Group = factor(c(2, 2, 2, 1, 1, 1)),
Date = as.Date(rep(c("2020-01-01", "2020-01-02", "2020-01-03"), times = 2)),
value = c(1, 2, 1, 5, 5.5, 6)
)
g <- ggplot(dat, aes(x = Date, y = value, color = Group)) +
geom_point() +
scale_color_brewer(type = "qual", palette = "Set1") +
geom_line(aes(group = Date), color = "black") +
theme_minimal()
ggplotly(g)
Here is an other example code I try to create the same plot using plotly. The final step I want to do is to add the horizontal lines like the previous plot.
plot_ly(dat, x = ~Date, y = ~value, color = ~factor(Group),
colors = "Set1",
type = "scatter", mode = "markers")
This appears to do
plot_ly(dat, x = ~Date, y = ~value, color = ~factor(Group),
colors = "Set1",
type = "scatter", mode = "markers") %>%
add_segments(data=dat,
x = ~Date[Group == 1],
xend= ~ Date[Group == 1],
y = ~value[Group == 1],
yend = ~value[Group ==2],
color = ~ Group[Group == 2],
line=list(color="black"),
showlegend=FALSE)
I was trying the following code in order to get a graph of boxplots with ggplot2 which are grouped according to different categories:
category_1 <- rep(LETTERS[1:4], each = 20)
value <- rnorm(length(category_1), mean = 200, sd = 20)
category_2 <- rep(as.factor(c("Good", "Medium", "Bad")), length.out = length(category_1))
category_3 <- rep(as.factor(c("Bright", "Dark")), length.out = length(category_1))
df <- data.frame( category_1, value, category_2, category_3)
p <- ggplot(df, aes(x = category_1, y = value, color = category_2, shape = category_3)) +
geom_boxplot(alpha = 0.5) +
geom_point(position=position_jitterdodge(), alpha=0.7)
p
I'm still too noob in stackoverflow to post images, but this is the result I want.
However, when I try to convert it to plotly using
pp <- ggplotly(p)
pp
the last 2 grouping layers (shape and color) are "ignored" and all the boxplots are plotted on top of each other, only respecting the x-axis grouping specified in aes(x = category_1, ...) as you can see here.
How can I avoid this problem? Thanks for your time.
EDIT
I've tried using plotly syntax directly and I get a similar result using the following code:
pp <- plot_ly(df, x = ~category_1, y = ~value, color = ~category_2,
mode = "markers", symbol = ~category_3, type = "box", boxpoints = "all") %>%
layout(boxmode = "group")
pp
Here the result. I said similar because plotly forces the dots to be next to, and not on top of the boxplot, which is not exactly what I wanted.
I guess the question is "solved". Although, I'm still curious if there is an explanation for the problem above. Thanks again!
I think this will solve your issue.
p <- ggplot(df, aes(x = category_1, y = value, color = category_2, shape = category_3)) +
geom_boxplot(alpha = 0.5) +
geom_point(position=position_jitterdodge(), alpha=0.7)
p %>%
ggplotly() %>%
layout(boxmode = "group")
Cheers.
I know similar questions asked before but my question is different. Consider data points data1 that have colors with respect to x and y coordinates and I plot it with ggplot
x = 1:100
y = 1:100
d = expand.grid(x,y)
data1 <- data.frame(
xval = d$Var1,
yval = d$Var2,
col = d$Var1+d$Var2)
data2 <- data.frame(
xnew = c(1.5, 90.5),
ynew = c(95.5, 4))
ggplot(data1, aes(xval, yval, colour = col)) + geom_point()
But I want the last line don't plot anything and I want plot data2 points with respect to colors of data1. for example I paint what I want to plot for data2 :
I changed the last line to:
ggplot(data1, aes(xval, yval, colour = col)) +
geom_point(data = data2, aes(x = xnew, y = ynew))
Now I expect that ggplot draw just 2 points of data2, but I have an Error:
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error: Column colour must be a 1d atomic vector or a list
The problem is, that there is no mapping between col out of data1 and your data2.
Please try the following:
ggplot(data2, aes(x = xnew, y = ynew, colour = xnew)) + geom_point() +
scale_fill_gradientn(colours=c(2,1),
values = range(data1$xval),
rescaler = function(x,...) x,
oob = identity)
I have data in following format.
X ID Mean Mean+Error Mean-Error
61322107 cg09959428 0.39158198 0.39733463 0.38582934
61322255 cg17147820 0.30742542 0.31572314 0.29912770
61322742 cg08922201 0.47443355 0.47973039 0.46913671
61322922 cg08360511 0.06614797 0.06750279 0.06479315
61323029 cg00998427 0.05625839 0.05779519 0.05472160
61323113 cg15492820 0.10606674 0.10830587 0.10382761
61323284 cg02950427 0.36187007 0.36727818 0.35646196
61323413 cg01996653 0.35582920 0.36276991 0.34888849
61323667 cg14161454 0.77930230 0.78821970 0.77038491
61324205 cg25149253 0.93585347 0.93948514 0.93222180
How can i plot error bar plot with column(bars)
enter image description here
where X-Axis is having X value. So each bar will be plotted at X of fixed width.
I'll try answering. I am using a package called plotly. You can look here for more details.
df <- read.csv('test.csv')
colnames(df) <- c("x", "id", "mean", "mean+error", "mean-error")
df$`mean+error` = df$`mean+error` - df$mean
df$`mean-error` = df$mean - df$`mean-error`
library(plotly)
p <- ggplot(df, aes(factor(x), y = mean)) + geom_bar(stat = "identity")
p <- plotly_build(p)
length(p$data)
p$layout$xaxis
plot_ly(df, x = 1:10, y = mean, type = "bar",
error_y = list(symmetric = F,
array = df$`mean+error`,
arrayminus = df$`mean-error`,
type = "data")) %>%
layout(xaxis = list(tickmode = "array",tickvals = 1:10,ticktext = df$x))
I get this:
The most popular approach would probably be using geom_errorbar() in ggplot2.
library("ggplot2")
ggplot(df, aes(x=ID, y = Mean)) +
geom_bar(stat="identity", fill="light blue") +
geom_errorbar(aes(ymin = Mean.Error, ymax = Mean.Error.1))
where Mean.Error and Mean.Error.1 are the header names for mean +/- error you get when you try to read in your example as text.
The two separate charts created from data.frame work correctly when created using the R plotly package.
However,
I am not sure how to combine them into one (presumably with the add_trace function)
df <- data.frame(season=c("2000","2000","2001","2001"), game=c(1,2,1,2),value=c(1:4))
plot_ly(df, x = game, y = value, mode = "markers", color = season)
plot_ly(subset(df,season=="2001"), x = game, y = value, mode = "line")
Thanks in advance
The answer given by #LukeSingham does not work anymore with plotly 4.5.2.
You have to start with an "empty" plot_ly() and then to add the traces:
df1 <- data.frame(season=c("2000","2000","2001","2001"), game=c(1,2,1,2), value=c(1:4))
df2 <- subset(df, season=="2001")
plot_ly() %>%
add_trace(data=df1, x = ~game, y = ~value, type="scatter", mode="markers") %>%
add_trace(data=df2, x = ~game, y = ~value, type="scatter", mode = "lines")
here is a way to do what you want, but with ggplot2 :-) You can change the background, line, points color as you want.
library(ggplot2)
library(plotly)
df_s <- df[c(3:4), ]
p <- ggplot(data=df, aes(x = game, y = value, color = season)) +
geom_point(size = 4) +
geom_line(data=df_s, aes(x = game, y = value, color = season))
(gg <- ggplotly(p))
There are two main ways you can do this with plotly, make a ggplot and convert to a plotly object as #MLavoie suggests OR as you suspected by using add_trace on an existing plotly object (see below).
library(plotly)
#data
df <- data.frame(season=c("2000","2000","2001","2001"), game=c(1,2,1,2),value=c(1:4))
#Initial scatter plot
p <- plot_ly(df, x = game, y = value, mode = "markers", color = season)
#subset of data
df1 <- subset(df,season=="2001")
#add line
p %>% add_trace(x = df1$game, y = df1$value, mode = "line")