Disable x-axis sorting in line chart - r

I have the following simple data.frame:
x <- data.frame(x = c(1, 3, 5, 2, 4, runif(10)),
y = c(1, 2, 3, 4, 5, runif(10)))
I want to make a plot showing both the scatter plot and connecting some of the points with a line, so I use:
plot_ly(data = x) %>%
add_markers(
x = ~x,
y = ~y
) %>%
add_lines(
x = ~x[1:5],
y = ~y[1:5]
)
However, the resulting line graph is sorted along the x-axis, while I want the line to follow the order found in the data.frame (shown in red below).
Is there any way of doing this? I've found similar questions on SO, but they all deal with categorical values.
I could obviously use paths, but to my understanding those only exist as shapes within layout(). I'm hoping for something which behaves like a trace: responds to hover actions, appears (and can be hidden) in the legend, etc.

I have just found a solution by using add_paths instead of add_lines.
plot_ly(data = x) %>%
add_markers(
x = ~x,
y = ~y
) %>%
add_paths(
x = ~x[1:5],
y = ~y[1:5]
)
Hope it solves your challenge.

Related

R Plotly linked subplot with percentage histogram and categories coloured

The Background
I am using the plotly API in R to create two linked plots. The first is a scatter plot and the second is a bar chart that should show the percentage of data belonging to each category, in the current selection. I can't make the percentages behave as expected.
The problem
The plots render correctly and the interactive selection works fine. When I select a set of data points in the top scatter plot, I would like to see the percentage of that selection that belongs to each category. Instead what I see is the percentage of points in that selection in that category that belong to that category, in other words always 100%. I guess this is because I set color = ~c which applies a grouping to the category.
The Example
Here is a reproducible example to follow. First create some dummy data.
library(plotly)
n = 1000
make_axis = function(n) c(rnorm(n, -1, 1), rnorm(n, 2, 0.25))
data = data.frame(
x = make_axis(n),
y = make_axis(n),
c = rep(c("A", "B"), each = n)
)
Create a sharedData object and supply it to plot_ly() for the base plot.
shared_data = data %>%
highlight_key()
baseplot = plot_ly(shared_data)
Make the individual panels.
points = baseplot %>%
add_markers(x = ~x, y = ~y, color = ~c)
bars = baseplot %>%
add_histogram(x = ~c, color = ~c, histnorm = "percent", showlegend = FALSE) %>%
layout(barmode = "group")
And put them together in a linked subplot with selection and highlighting.
subplot(points, bars) %>%
layout(dragmode = "select") %>%
highlight("plotly_selected")
Here is a screenshot of this to illustrate the problem.
An Aside
Incidentally when I set histnorm = "" in add_histogram() then I get closer to the expected behaviour but I do want percentages and not counts. When I remove color = ~c then I get closer to the expected behaviour but I do want the consistent colour scheme.
What have I tried
I have tried manually supplying the colours but then some of the linked selection breaks. I have tried creating a separate summarised data set from the sharedData object first and then plotting that but again this breaks the linkage between the plots.
If anyone has any clues as to how to solve this I would be very grateful.
To me it seems the behaviour you are looking for isn't implemented in plotly.
Please see schema(): object ► traces ► histogram ► attributes ► histnorm ► description
However, here is the closest I was able to achive via add_bars and perprocessing the data (Sorry for adding data.table, you will be able to do the same in base R, just personal preference):
library(plotly)
library(data.table)
n = 1000
make_axis = function(n) c(rnorm(n, -1, 1), rnorm(n, 2, 0.25))
DT = data.table(
x = make_axis(n),
y = make_axis(n),
c = rep(c("A", "B"), each = n)
)
DT[, grp_percent := rep(100/.N, .N), by = "c"]
shared_data = DT %>%
highlight_key()
baseplot = plot_ly(shared_data)
# Make the individual panels.
points = baseplot %>%
add_markers(x = ~x, y = ~y, color = ~c)
bars = baseplot %>%
add_bars(x = ~c, y = ~grp_percent, color = ~c, showlegend = FALSE) %>%
layout(barmode = "group")
subplot(points, bars) %>%
layout(dragmode = "select") %>%
highlight("plotly_selected")
Unfortunately, the resulting hoverinfo isn't really desirable.

Bar-plot does not show bar for only one x value

I am having an issue with a plotly bar plot when I define the date range for the x-axis.
When there is one or more data points with the same x-value, the bars do not show in the plot. If there is at least two different x-values or if I do not use a x-axis range, then the bars show as they should.
Below follows an example (I am currently using lubridate to deal with dates).
library(lubridate)
library(plotly)
# Same x-value: bar does not show
plot_ly(x = c(ymd("2019-08-25"), ymd("2019-08-25")), y = c(1, 2), type = "bar") %>%
layout(xaxis = list(range = ymd(c("2019-08-20", "2019-08-30"))))
# Different x-values: bars are shown
plot_ly(x = c(ymd("2019-08-25"), ymd("2019-08-26")), y = c(1, 2), type = "bar") %>%
layout(xaxis = list(range = ymd(c("2019-08-20", "2019-08-30"))))
# No x-axis range defined, same x-values: the bar is shown
plot_ly(x = c(ymd("2019-08-25"), ymd("2019-08-25")), y = c(1, 2), type = "bar")
Any solution?
Edit: For comparison, ggplot2 does not have the same issue:
# ggplot works like expected
library(lubridate)
library(ggplot2)
ggplot(NULL, aes(x = ymd(c("2019-08-25", "2019-08-25")), y = c(1, 2))) +
geom_col() +
xlim(ymd(c("2019-08-20", "2019-08-30")))
Your code is actually being understood in your first version, but you need to set the width of the bars so they show up in the end.
I'm not sure what the units are (maybe miliseconds???) so you may need to play around with it or do research to get a good width for your actual scenario.
plot_ly() %>%
add_bars(x = c(ymd("2019-08-25"), ymd("2019-08-25")), y = c(1, 2), type = "bar",width=100000000)%>%
layout(xaxis = list(range = ymd(c("2019-08-20", "2019-08-30"))))

scatter3d - colour dots based on 4th variable

I'd like to do a 3D plot with scatter3d but don't know how to colour the dots based on the values of a fourth variable (here VAR4). Could someone kindly help me? It would be great if by adding these colours, I still keep the fading effect that is in the default version (the points the further in the 3D plot appear with a lighter colour). Thank you!
df <- data.frame(VAR4=c(10,52,78,34,13,54),
A=c(12, 8, 10, 7, 13, 15),
B=c(4,3,2,1,7,5),
C=c(1,3,2,1,3,1))
library(rgl)
library(car)
scatter3d(A ~B + C,color=VAR4, data=df, surface=F)
You can try this:
library(rgl)
library(car)
# add as group the VAR4, making as factor
scatter3d(A ~B + C, groups = as.factor(df$VAR4), data=df, surface=F)
But, if I could advice you, it's more pretty and nice to use the plotly package:
library(plotly)
# in this case we use VAR4 as continuous, you can put color = ~as.factor(VAR4) to have it as factors
plot_ly(df, x = ~A, y = ~B, z = ~C, color = ~VAR4) %>%
add_markers() %>%
layout(scene = list(xaxis = list(title = 'A'),
yaxis = list(title = 'B'),
zaxis = list(title = 'C')))

Error bars in plot_ly

I'm creating a simple plot with plot_ly and come to a strange behaviour when using error bars. I have tried the example here, but even the official manual is wrong (in my opinion). Here an MWE:
library(plotly)
df <- data.frame(
x = 1:3,
y = c(7,5,9),
sd = c(0.2, 0.1, 0.7))
plot_ly(df,
x = ~x,
error_y = list(value = ~sd)) %>%
add_markers(y = ~y)
The result is not the expected plot, because the errorbars are always 10% of the value, even in the official examples (see link above). It's obvious that the errorbar are much higher than the given ones in df. The error are always 10 % of the original value.
I tried different approaches, e.g. error_y = list(value = ~sd, type = "data")) (seen here), but nothing worked.
I'm thankful for every hint to solve this issue.
It seems that the Plotly team has forgotten to update its own examples. The correct syntax for getting error bars in R Plotly is
error_y = list(array=~sd)
library(plotly)
df <- data.frame(
x = c(1, 2, 3),
y = c(7, 5, 3),
sd = c(0.1, 0.3, 0.8))
plot_ly(df,
x = ~x,
y = ~y,
error_y = list(array=~sd),
type='scatter')

confused about grouping points in ggplot2

Why doesn't this code make lines between data at the same values of y?
main <- data_frame(x=rep(c(-1, 1), each=2), y = c(c(1, 1), c(2, 2)), z = c(1, 2, 3, 4))
qplot(data = main, x = x, y = z, geom="line", group=factor(y))
Here is what I get:
But I want only the points at the same level of y to be connected.
The issue is with how you defined your y variable. Change it to y = c(c(1,2), c(1,2)) and things should work.
Also, if you're going to use data_frame be sure to add the calls to library to make your code reproducible (i.e., library(dplyr) and library(ggplot2)).

Resources