Avoid converting numbers to dates in plotly - r

I have a matrix that I want to create a heatmap for in plotly. the row names are assays and the colnames are CASRN and they are in this format "131-55-5"
my matrix looks like this
the data matrix for the heatmap
for some reason plotly thinks these are dates and converts them to something like March 2000 and gives me an empty plot.
before i convert my data frame to matrix i checked and all columns are factors.
is there any way I can make sure my numbers wont turn into dates when i plot my matrix?
this is the code i am using for my heatmap
plot_ly(x=colnames(dm_new2), y=rownames(dm_new2), z = dm_new2, type = "heatmap") %>%
layout(margin = list(l=120))

Using some random data to mimic your dataset. Simply put your matrix in a dataframe. Try this:
set.seed(42)
library(plotly)
library(dplyr)
library(tidyr)
dm_new2 <- matrix(runif(12), nrow = 4, dimnames = list(LETTERS[1:4], c("131-55-5", "113-48-4", "1582-09-8")))
# Put matrix in a dataframe
dm_new2 <- as.data.frame(dm_new2) %>%
# rownames to column
mutate(x = row.names(.)) %>%
# convert to long format
pivot_longer(-x, names_to = "y", values_to = "value")
dm_new2 %>%
plot_ly(x = ~x, y = ~y, z = ~value, type = "heatmap") %>%
layout(margin = list(l=120))
Created on 2020-04-08 by the reprex package (v0.3.0)

Related

using dygraph library in r to plot dual axis

I am trying to plot two series at different scales on same plot with dygraph lib in r.
dygraph(data.frame(x = 1:10, y = runif(10),y2=runif(10)*100)) %>%
dyAxis("y", valueRange = c(0, 1.5)) %>%
dyAxis(runif(10)*100,name="y2", valueRange = c(0, 100)) %>%
dyEvent(2, label = "test") %>%
dyAnnotation(5, text = "A")
however, The plot does not fit the data with larger scale, I cannot figure out how to align the two axises. I suspect the option independentTicks in dyAxis() function does the trick but I cannot find how to use it in the documentation. Please help out with this. Best
One way could be:
We pass the named vector of the column with higher values to dySeries function:
See here https://rstudio.github.io/dygraphs/gallery-axis-options.html
library(dygraphs)
library(dplyr)
df = data.frame(x = 1:10, y = runif(10),y2=runif(10)*100)
y2 <- df %>%
pull(y2)
names(y2) <- df$x
dygraph(df) %>%
dySeries("y2", axis = 'y2')

Converting basic r barplot to ggplot

I've currently got a barplot that has a few basic parameters. However, I'm looking to try and convert this into ggplot. The extra parameters don't matter too much; the main problem that I'm having is that I'm trying to plot the sum of various columns, but I'm unable to transpose it correctly as t(data) doesn't seem to work. Here's what I've got so far:
## Subset of indicators
indicators <- clean_data[c(8, 12, 14:23)]
## Get sum of columns
indicator_sums <- colSums(indicators, na.rm = TRUE)
### Transpose for ggplot
(empty)
## Make bar plot
barplot(indicator_sums, ylim=range(pretty(c(0, indicator_sums))), cex.axis=0.75,cex.lab=0.8, cex.names=0.7, col='magenta', las=2, ylab = 'Offences Recorded Using Indicator')
You may try
library(dplyr)
library(reshape2)
dummy <- data.frame(
A = c(1:20),
B = rnorm(20, 10, 4),
C = runif(20, 19,30),
D = sample(c(10:40),20, replace = T)
)
barplot(colSums(dummy))
dummy %>%
colSums %>%
melt %>%
rownames_to_column %>%
ggplot(aes(x = rowname, y = value)) +
geom_col()

Plot multiple lists on the same graph in r (scatter plot)

I was trying to plot a graph that looks like the below figure based on the code under it:
xAxisName <- c("ML", "MN")
car1 <- c(5,6)
names(car1) <- xAxisName
car2 <- c(5.5,6.2)
names(car2) <- xAxisName
car3 <- c(4.9, 5.4)
names(car3) <- xAxisName
The plot plots 2 car properties on the x axis and each property has 3 car values. But these are separate lists. How could this plot be plotted?
Get all the 'car' objects into a list, bind them with bind_rows and use ggplot, then pivot to 'long' format and use ggplot
library(ggplot2)
library(dplyr)
library(tidyr)
mget(ls(pattern = '^car\\d+$')) %>%
bind_rows(.id = 'car') %>%
pivot_longer(cols = -car) %>%
ggplot(aes(x = name, y = value, color = car)) +
geom_point()+
scale_y_continuous(expand = c(5, 6))

Line plotting on small dataset

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)
)

How to change values in a data.frame column into numbers?

I have the following (sample) data.frame
x <- data.frame(gene = 1:3, Sample1 = 5:7, Sample2 = 4:6, Sample3 = 6:8)
I want to change the column names and then use the numbers in the new titles as x-axis values for my plot
colnames(x) <- c("Gene", "HeLa_0.2", "HeLa_2.0", "HeLa_5.0")
x_gather <- x %>%
gather(key=treatment, value=values, -c(Gene)) %>%
tidyr::separate(treatment, into=c("Cell_line", "treatment"),sep="_")
ggplot()+
geom_line(x_gather, mapping=aes(treatment, y=values, group=Gene))
But I want the numbers to be spaced on an x-axis like this, instead of on an axis like this (which I get only if I copy my data to excel, format them as numbers, and then load it into R again...)
Any suggestions to how to solve this?
Thanks! :)
All you need to do is make the treatment variable numeric. For instance:
x_gather <- x %>%
gather(key=treatment, value=values, -c(Gene)) %>%
tidyr::separate(treatment, into=c("Cell_line", "treatment"),sep="_") %>%
mutate(treatment = as.numeric(treatment))
ggplot()+
geom_line(x_gather, mapping=aes(treatment, y=values, group=Gene))

Resources