Related
I've been trying to create a volcano plot using the muma package in r but I've had some difficulties in importing my data in CSV format, every time I try to run the code (below) I get this error message.
If you know an easier way to create a volcano plot, using a different package, it would really help me.
thanks
explore.data(file="datosFin_A1AT.csv",scaling="Auto", scal = TRUE, normalize = TRUE,
imputation = TRUE, imput="mean")
Error in [.data.frame(comp, , 3:ncol(comp)) :
undefined columns selected
structure(list(Muestra = c("MI-001", "MI-003", "MI-009", "MI-012"), Class = c("Presencia",
"Ausencia", "Presencia", "Ausencia"), Per_Cintura = c(97.6, 92.8, 98.8, 113.4), HDL = c(38, 51, 51, 44), TG = c("195", "76", "160", "128"), ApoB = c(145, 161, 173, 50.9), Glucosa_mg=c(86, 85, 96, 79, 7), LBP = c(443.187, 438.925, 703.752,
540.541), IFABP = c(0.705485, 0.906843, 144.873, 145.884), CLD3 = c(0.2, 501.596, 315.582, 446.307), Acetico = c(NA, 745.654, NA, 105.378), Propionico = c(NA, 682.719, 86.628, 303.139), Butirico = c(NA, 571.421, 265.559, 135.674), Isobutirico = c(286.085, 0.0381631, 0.276992, 0.0467809), prevotella = c(0.12843, 0.07927, 0.22459, 0.01726), Pathogen=c(0.05639, 0.16051, 0.01617, 0.04398), Lachnospiraceae = c(0.24202, 0.73606, 0.67789, 0.62656), Aker_Bacter = c(0.06167, 0.00999, 0.03426, 0.0211), Ruminoco = c(0.33593, 3e-05, 0.01538, 0.01298), TNFa = c(14.16, 35.35, 43.71, 42.99), PCR = c(1.71, 1.84, 3.52, 2.32), IL33 = c(148.7, 207.6, 146.2, 162.6), IL8 = c(157.9, 115.3, NA, NA), IL1b = c(13.68, 12.36, 13.69, 19.06), IL18 = c(231.6, 293.5, 366.2, 298.5)))
My goal is to create a bar graph of categorical variables and then add a pie chart inside it as shown in the attached image.
my data is the same as in the image and is below:
#For bar grapgh
chromosomes = c("1A", "1B", "1D", "2A", "2B", "2D", "3A", "3B", "3D", "4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668, 752, 213, 826, 948, 334, 625, 834, 264, 488, 391, 136, 745, 917, 234, 543, 848, 182, 901, 740, 241)
data_bar <- data.frame(chromosomes, Frequency)
#For pie chart
Genome = c("A","B","D")
Count = c(4796, 5430, 1604)
data_pie <- data.frame(Genome, Count)
I will highly appreciate if anyone can guide me or direct me towards where I can find the answers
Here's a ggplot2 solution:
pie <- ggplot(data_pie, aes(x = 1, y = Count, fill = Genome)) +
geom_col(color = "black") +
scale_fill_manual(values = c("red2", "forestgreen", "dodgerblue3")) +
coord_polar(theta = "y") +
theme_void() +
theme(legend.position = "none")
ggplot(data_bar) +
geom_col(aes(chromosomes, Frequency, fill = substr(chromosomes, 2, 2)),
color = "black", width = 0.5) +
scale_fill_manual(values = c("red2", "forestgreen", "dodgerblue3")) +
theme_classic() +
theme(legend.position = "none") +
annotation_custom(ggplotGrob(pie), xmin = "2B", xmax = "6A", ymin = 500)
Using only base R functions, please see the code below:
## Your data ---------------------------------
#For bar grapgh
chromosomes = c("1A", "1B", "1D", "2A", "2B", "2D", "3A", "3B", "3D", "4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668, 752, 213, 826, 948, 334, 625, 834, 264, 488, 391, 136, 745, 917, 234, 543, 848, 182, 901, 740, 241)
#For pie chart
Genome = c("A","B","D")
Count = c(4796, 5430, 1604)
## One idea to start with --------------------
plot.new()
par(mfrow = c(2, 1), # set the layout, you might also consider layout() or split.screen() as suggested in ?par
mar=c(4, 4, 1, 1), # this set up give enough space to see axis labels and titles
oma=c(0, 0, 0, 0)) # remove outer margins
pie(Count, labels = Genome)
barplot(Frequency~chromosomes)
Output:
I think it is possible to make it look cleaner adjusting par() arguments but I am not very familiar with them.
There are also packages cowplot and gridExtra that works nicely with ggplot2.
Let's say, I have a simple polar chart:
R code:
library(plotly)
p <- plot_ly(
type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers'
) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE#,
#tickmode="array",
#tickvals = c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5),
#ticktext = c('A', "B", "C", "D", "E", "F", "G", "H")
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Chart plotted:
When I set showticklabels = FALSE, the angle tick labels disappears, and then I want to put A,B,C,D,E,F,G and H at angles c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5).
I am not able to get the below expected plot using ticktexts and tickvals.
Can someone please help me with getting me to the solution, or if it is possible with add_text or add_annotation ?
Expected plot:
you could remove all grid lines altogether in the angularaxis with showgrid = FALSE, or you can have a line per 22.5 degree starting from 0 and then the ticktext would be something like this c('', 'A', '', 'B', '', 'C', .......) , or you could tediously add the lines you expect to have and then remove the grid lines like this:
p <- plot_ly() %>%
# data points
add_trace(type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers') %>%
# straight line from 0 dg to 180 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 180, 360),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 45 dg to 225 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 45, 225),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 90 dg to 270 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 90, 270),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 135 dg to 315 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 135, 315),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# fill circle of radius 1
add_trace(type = 'scatterpolar',
mode = 'lines',
r = 1,
theta =seq(0, 360, 0.1),
line = list(color = 'grey'),
fill = 'toself',
fillcolor = 'grey',
opacity = 0.5) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE,
# remove grid lines and ticks
showgrid = FALSE,
ticks = '',
# if you want the axis to go the other way around
# direction = 'clockwise',
tickmode="array",
tickvals = seq(22.5, 337.5, 45),
ticktext = LETTERS[1:8]
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Output chart:
I noticed that the expected output you have there, lists the letters the other way around as you have them in your code. If this is something you also want just change the order of the letters to match the angles like this c("A", "H", "G", "F", "E", "D", "C", "B") (reversed order starting from A)
I am trying to create a Euler diagram in R using the eulerr package. I would like to reduce the font size of the quantities text on the plot.
I have tried using cex=0.5 (as per example below) and have also tried fontsize = and font = but none have reduced the font size. Am I placing cex=0.5 in the wrong position?
library(eulerr)
set1 <- euler(c("A&B" = 3103,
"A&C" = 1034,
"A&D" = 118,
"B&C" = 2690,
"B&D" = 1017,
"C&D" = 1383,
"A&B&C" = 394,
"A&B&D" = 73,
"A&C&D" = 45,
"B&C&D" = 153,
"A&B&C&D" = 32))
eulerr.plot <- plot(set1,
fills = list(fill = c("#009292", "#FFB6DB", "#B66DFF", "#6DB6FF"), alpha = 0.7),
labels = NULL, quantities = TRUE, legend = list(labels = c("A", "B", "C", "D")), cex = 0.5)
You can change the text size of the quantities by passing a list defining the size to that argument like so:
plot(set1, fills = list(fill = c("#009292", "#FFB6DB", "#B66DFF", "#6DB6FF"), alpha = 0.7),
labels = NULL, quantities = list(cex = .5), legend = list(labels = c("A", "B", "C", "D")))
I got this problem I still haven't found out how to solve it. I want to plot all the Values MW1, MW2 and MW3 in function of "DHT + Procymidone". How can I plot all this values in the graphic so that I will get 3 different curves (in different colors and different number like curve 1, 2, ...)? And I want the labels of the X-Values("DHT + Procymidone") to be like -10, -9, ... , -4 instead of 1,00E-10, ...
DHT + Procymidone MW 1 MW 2 MW 3
1,00E-10 114,259526780335 111,022461066274 213,212408408682
1,00E-09 115,024187788314 111,083316791613 114,529425136628
1,00E-08 110,517449986348 107,867941606743 125,10230718665
1,00E-07 100,961311263444 98,4219995773135 116,045168653416
1,00E-06 71,2383604211297 73,539659636842 50,3213799775309
1,00E-05 20,3553333652104 36,1345771905088 15,42260866106
1,00E-04 4,06189509055904 18,1246447874679 10,1988107887318
I have shortened your data frame for convenience reasons, so here's an example:
mydat <- data.frame(DHT_Procymidone = c(-10, -9, -8, -7, -6, -5, -4),
MW1 = c(114, 115, 110, 100, 72, 20, 4),
MW2 = c(111, 111, 107, 98, 73, 36, 18),
MW3 = c(213, 114, 123, 116, 50, 15, 10))
library(tidyr)
library(ggplot2)
mydf <- gather(mydat, "grp", "MW", 2:4)
ggplot(mydf, aes(x = DHT_Procymidone, y = MW, colour = grp)) + geom_line()
which gives following plot:
To use ggplot, your data needs to be in long-format. gather does this for you, appending columns MW1-MW3 into one column, while the column names are added as new column values in the grp-column. This group-column allows to identify different groups, i.e. different colored lines in the plot.
Depending on the type of DHT + Procymidone, you can, e.g. use format(..., scientific = FALSE) to convert to numeric, however, this will result in -0.0000000001 (and not -10).
However, if this data column is a character vector (you can coerce with as.character), this may work:
a <- "1,00E-10"
sub("1,00E", "", a, fixed = TRUE)
> [1] "-10"
As an alternative answer to #Daniel's which doesn't rely on ggplot (thanks Daniel for providing the reproducible data).
mydat <- data.frame(DHT_Procymidone = c(-10, -9, -8, -7, -6, -5, -4),
MW1 = c(114, 115, 110, 100, 72, 20, 4),
MW2 = c(111, 111, 107, 98, 73, 36, 18),
MW3 = c(213, 114, 123, 116, 50, 15, 10))
plot(mydat[,2] ~ mydat[,1], typ = "l", ylim = c(0,220), xlim = c(-10,-2), xlab = "DHT Procymidone", ylab = "MW")
lines(mydat[,3] ~ mydat[,1], col = "blue")
lines(mydat[,4] ~ mydat[,1], col = "red")
legend(x = -4, y = 200, legend = c("MW1","MW2","MW3"), lty = 1, bty = "n", col = c("black","blue","red"))
To change axis labels see the text in xlab and ylab. To change axis limits see xlim and ylim.