rcharts nvd3 linechart with categorical x axis - r

How can I a draw a linechart with rcharts using nvd3 and a categorical xaxis? What I'm trying to do would look with ggplot2 like this:
library(ggplot2)
dat <- data.frame(expand.grid(group = letters[1:3], x = letters[4:6]), y = rnorm(9))
ggplot(dat, aes(x = x, y = y, group = group, color = group)) + geom_line()
I tried:
library(rCharts)
nPlot(y ~ x, group = 'group', data = dat, type = 'lineChart')
I know that I could change x to a numeric variable, but then the xaxis labels would not be correctly written.

is this what you are after?
foo <- hPlot(x = "x", y = "y", groups = "group", data = dat, type = "line")
foo

Related

How can I successively add multiple lines to a graph in a plotly animation?

I want to show multiple lines being added to a plotly plot (as an animation) using R. For example, I have the following plotly line graphs (p, p2, p3):
library(plotly)
set.seed(3)
x = 1:10
y = 1:10
y2 = y^2
y3 = y^3
p = plot_ly(data = data.frame(x = x, y = y), x = ~ x, y = ~y, type = "scatter", mode = "lines")
p2 = plot_ly(data = data.frame(x = x, y = y2), x = ~ x, y = ~y2, type = "scatter", mode = "lines")
p3 = plot_ly(data = data.frame(x = x, y = y3), x = ~ x, y = ~y3, type = "scatter", mode = "lines")
Here p, p2, p3 are different plots but they all have the same x axis and different y axis. I want to be able to make an animation where the lines y, y2, y3 will successively appear in the plotly graph.
P.S: It does not strictly have to be done using plotly, but strongly preferred.
An idea might be to create a 'dataset' for each frame.
The first frame contains all values for y and all values for y2 and y3 are located outside the y-axis limits. For the second frame all values from y and y2 are shown and just the values from y3 are beyond the limit. In frame 3 all values are included.
library(tidyverse)
library(plotly)
# transform dataframe into a long format
df <- data.frame(x = 1:10,
y = 1:10) %>%
mutate(y2 = y^2,
y3 = y^3) %>%
pivot_longer(cols = -x,
names_to = "line",
values_to = "value")
# set the values for each frame and line
# (all lines not shown, need to hidden outside the plot limits - NA won't work)
df_plot <- map_df(1:3, ~ mutate(df, frame = .)) %>%
mutate(value = case_when(frame == 1 & line %in% c("y2", "y3") ~ -10,
frame == 2 & line %in% c("y3") ~ -10,
TRUE ~ value))
# create plot
plot_ly(data = df_plot,
x = ~x,
y = ~value,
color = ~line,
type = "scatter",
mode = "line",
frame = ~frame) %>%
layout(yaxis=list(range = c(0, 1000))) %>%
animation_opts(easing = "bounce")

paired segments in ggplot

Given foo I'd like to connect pairs of points so that there is a line between foo[1,] and foo[11,], foo[2,] and foo[12,], up to foo[10,] and foo[20,]. Seems like this should be possible with an artful call to geom_segment and the grouping variable id?
foo <- data.frame(id = c(1:10,1:10),
samp = rep(c("A","B"),each=10),
x = c(rnorm(10,mean = 5),rnorm(10,mean = 5)),
y = c(rnorm(10,mean = 5),rnorm(10,mean = 6)))
ggplot(foo,aes(x=x,y=y,col=samp)) + geom_point()
Any help appreciated.
We need to use the group as 'id' in the geom_line
library(ggplot2)
ggplot(foo) +
geom_point(aes(x = x,y = y, col = samp)) +
geom_line(aes(x = x, y = y, group = id))
-output

ggplot missing plot with x-axis factor

The following works fine:
my_df <- data.frame(x_val = 1:10, y_val = sample(1:20,10),
labels = sample(c("a", "b"), 10, replace = T))
ggplot(data = my_df, aes(x = x_val, y = y_val)) + geom_line()
but if I chance x_val to factor, I am getting blank plot and message:
my_df <- data.frame(x_val = 1:10, y_val = sample(1:20,10),
labels = sample(c("a", "b"), 10, replace = T))
my_df$x_val <- as.factor(my_df$x_val)
ggplot(data = my_df, aes(x = x_val, y = y_val)) + geom_line()
message:
geom_path: Each group consists of only one observation. Do you
need to adjust the group aesthetic?
I can obviously drop factor conversion, but I need it in order to replace labels of x axis with scale_x_discrete(breaks = 1:10,labels= my_df$labels). Here is where I borrowed it link
Any thoughts?
Can you just leave x_val as numeric and use scale_x_continuous(breaks = 1:10,labels= my_df$labels) instead?

How plot new point in ggplot with older color data?

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)

How to plot bar plot and error bar with x,y data in R

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.

Resources