i have this small dataset that i need to plot with plotly. Im struggling with it :/
Im looking to have 3 colored lines (each line for each of the rows (1,2,3). The x axis needs to be the column names and the Y axis represents each one of the numeric values.
My code so far looks wrong
plot_ly (x = colnames(a), y = a[1], type = "scatter" ,mode = "lines" )
I'm not sure that this is your desired plot, but it sounded closest to your description. I adapted a few columns of your data to describe.
The plot will be easier if data is in longer form with pivot_longer. Also easier if you add row numbers to your data so you can plot one line for each row number.
Since plotly will plot your xaxis categories alphabetically, you will want to relevel your name factor (name is your column names) to order of your columns.
In your plot_ly statement, use split to plot by row number.
library(plotly)
library(tidyverse)
a %>%
mutate(rn = row_number()) %>%
pivot_longer(cols = -rn, names_to = "name", values_to = "value") %>%
mutate(name = factor(name, levels = colnames(a))) %>%
plot_ly(x = ~name, y = ~value, split = ~rn, type = "scatter", mode = "lines")
Output
Data
a <- data.frame(
N_of_Brands = c(-.4, .8, -.4),
Brand_Runs = c(-.26, .70, -.75),
Total_Volume = c(-.69, .15, -.015),
No_of_Trans = c(-.81, .45, -.35)
)
I am trying to create the top left graph in this figure in ggplot, using viridis to make the colour gradient.
Here is my sample data:
# simulate t-values
data = data.frame(sim =1:10000,
t_0= rt(n = 10000,df =12, ncp=0),
t_1 = rt(n = 10000,df =12, ncp=1.2))
# compute p-values
data = data %>%
mutate(p_0 = 2* pt(t_0, df=12, lower.tail = ifelse(t_0 > 0,FALSE ,TRUE)),
p_1 = 2* pt(t_1, df=12, lower.tail = ifelse(t_1 > 0,FALSE ,TRUE)))
# convert from wide to long
data.long = data %>%
gather(condition,measurement, t_0:p_1) %>%
separate(col=condition, into=c("para","hyp"), sep = "_")
# convert to wide repeated measures format
data.wide = data.long %>% spread(key = para, measurement)
To create the graphs on the left, I need to colour the histogram according to the corresponding values in the right graphs. If t = 0 (corresponding to a p close to 1), the graph should be yellow, if t>4 (corresponding to a p close to 0), the fill should be dark blue. This post shows how to create a similar graph using scale_fill_gradientn, which does unfortunately does not work with the discrete values I have created using cut().
This is the closest I have come, however I want the graph to have yellow for x=0 blending to dark blue at the edges.
# create bins based on t-values
t0bins <- seq(-12, 12, by = 1)
# compute corresponding p-values
pt0bins <- 2*pt(t0bins, df = 12, lower.tail = FALSE)
ggplot(data.wide, aes(x=t, fill=cut(..x.., breaks=get("t0bins", envir=.GlobalEnv)))) +
geom_histogram(binwidth=0.1)+
scale_fill_viridis(discrete=T)
which gives:
You can try
library(tidyverse)
library(viridis)
data.wide %>%
mutate(bins=cut(t, breaks=t0bins)) %>%
{ggplot(.,aes(x=t, fill=bins)) +
geom_histogram(binwidth=0.1)+
scale_x_continuous(limits =c(-12,12)) +
scale_fill_manual(drop=FALSE,values = c(viridis(nlevels(.$bins)/2), viridis(nlevels(.$bins)/2, direction = -1)))}
I am trying to plot multiple box plots as a single graph. The data is where I have done a wilcoxon test. It should be like this
I have four/five questions and I want to plot the respondent score for two sets as a box plot. This should be done for all questions (Two groups for each question).
I am thinking of using ggplot2. My data is like
q1o <- c(4,4,5,4,4,4,4,5,4,5,4,4,5,4,4,4,5,5,5,5,5,5,5,5,5,3,4,4,3,4)
q1s <- c(5,4,4,5,5,5,5,5,4,5,4,4,5,4,5,5,5,5,5,5,5,5,5,5,5,5,4,5,4,4)
q2o <- c(3,3,3,4,3,4,4,3,3,3,4,4,3,4,3,3,4,3,3,3,3,4,4,4,4,3,3,3,3,4)
q2s <- c(5,4,4,5,5,5,5,5,4,5,4,4,5,4,5,5,5,5,5,5,5,5,5,5,5,5,4,3,4,4)
....
....
q1 means question 1 and q2 means question 2. I also want to know how to align these stacked box plots based on my need. Like one row or two rows.
This should get you started:
Unfortunately you don't provide a minimal example with sample data, so I will generate some random sample data.
# Generate sample data
set.seed(2017);
df <- cbind.data.frame(
value = rnorm(1000),
Label = sample(c("Good", "Bad"), 1000, replace = T),
variable = sample(paste0("F", 5:11), 1000, replace = T));
# ggplot
library(tidyverse);
df %>%
mutate(variable = factor(variable, levels = paste0("F", 5:11))) %>%
ggplot(aes(variable, value, fill = Label)) +
geom_boxplot(position=position_dodge()) +
facet_wrap(~ variable, ncol = 3, scale = "free");
You can specify the number of columns and rows in your 2d panel layout through arguments ncol and nrow, respectively, of facet_wrap. Many more details and examples can be found if you follow ?geom_boxplot and ?facet_wrap.
Update 1
A boxplot based on your sample data doesn't make too much sense, because your data are not continuous. But ignoring that, you could do the following:
df <- data.frame(
q1o = c(4,4,5,4,4,4,4,5,4,5,4,4,5,4,4,4,5,5,5,5,5,5,5,5,5,3,4,4,3,4),
q1s = c(5,4,4,5,5,5,5,5,4,5,4,4,5,4,5,5,5,5,5,5,5,5,5,5,5,5,4,5,4,4),
q2o = c(3,3,3,4,3,4,4,3,3,3,4,4,3,4,3,3,4,3,3,3,3,4,4,4,4,3,3,3,3,4),
q2s = c(5,4,4,5,5,5,5,5,4,5,4,4,5,4,5,5,5,5,5,5,5,5,5,5,5,5,4,3,4,4));
df %>%
gather(key, value, 1:4) %>%
mutate(
variable = ifelse(grepl("q1", key), "F1", "F2"),
Label = ifelse(grepl("o$", key), "Bad", "Good")) %>%
ggplot(aes(variable, value, fill = Label)) +
geom_boxplot(position = position_dodge()) +
facet_wrap(~ variable, ncol = 3, scale = "free");
Update 2
One way of visualising discrete data would be in a mosaicplot.
mosaicplot(table(df2));
The plot shows the count of value (as filled rectangles) per Variable per Label. See ?mosaicplot for details.
df <- data.frame(X1 = rep(1:5,1), X2 = rep(4:8,1), var1 = sample(1:10,5), row.names = c(1:5))
library("ggvis")
graph <- df %>%
ggvis(~X1) %>%
layer_lines(y = ~ var1) %>%
add_axis("y", orient = "left", title = "var1") %>%
add_axis("x", orient = "bottom", title = "X1") %>%
add_axis("x", orient = "top", title = "X2" )
graph
Obviously, the top x-axis (X2) is not correct here since it refers to the same variable as X1. I know how to create a scaled dual-y axis in ggvis. But how can I create a similar dual axis on different X? Those two X-axis should refer to different variables (X1 and X2 in this example).
I know this could be a really BAD idea to make dual X-axis. But one of my working dataset may need me to do so. Any comments and suggestions are appreciated!
The second axis needs to have a 'name' in order for the axis to know which variable to reflect. See below:
df <- data.frame(X1 = rep(1:5,1),
X2 = rep(4:8,1),
var1 = sample(1:10,5),
row.names = c(1:5))
library("ggvis")
df %>%
ggvis(~X1) %>%
#this is the line plotted
layer_lines(y = ~ var1) %>%
#and this is the bottom axis as plotted normally
add_axis("x", orient = "bottom", title = "X1") %>%
#now we add a second axis and we name it 'x2'. The name is given
#at the scale argument
add_axis("x", scale = 'x2', orient = "top", title = "X2" ) %>%
#and now we plot the second x-axis using the name created above
#i.e. scale='x2'
layer_lines(prop('x' , ~X2, scale='x2'))
And as you can see the top x-axis reflects your X2 variable and ranges between 4 and 8.
Also, as a side note: You don't need rep(4:8,1) to create a vector from 4 to 8. Just use 4:8 which returns the same vector.
My data looks something like this:
df = data.frame(name=c("A1", "A2"),
x = c(2,4),
y = c(2,5),
sector = c("blue", "red"))
I am trying to use ggvis to create a graph but I am not able to make the tooltip work.
library(ggvis)
df %>%
ggvis(~x, ~y, size := 100, opacity := 0.4) %>%
layer_points(fill = ~sector) %>%
add_tooltip(function(df) df$name)
When I hover the mouse df$name does not appear. What am I doing wrong?
Thanks!
The helpfile for add_tooltip has a clue:
The data sent from client to the server contains only the data columns
that are used in the plot. If you want to get other columns of data,
you should to use a key to line up the item from the plot with a row
in the data.
My fix below adapts the example from that helpfile.
library(ggvis)
df = data.frame(name=c("A1", "A2"),
x = c(2,4),
y = c(2,5),
sector = c("blue", "red"))
# Add a unique id column
df$id <- 1:nrow(df)
# Define a tooltip function, which grabs the data from the original df, not the plot
tt <- function(x) {
if(is.null(x)) return(NULL)
# match the id from the plot to that in the original df
row <- df[df$id == x$id, ]
return(row$name)
}
# in the definition of the plot we include a key, mapped to our id variable
df %>%
ggvis(~x, ~y, key := ~id, size := 100, opacity := 0.4) %>%
layer_points(fill = ~sector) %>%
add_tooltip(tt, "hover")