R boxplot with several variables - changing variable names on x-axis - r

I am new to R and having issues figuring out how to plot multiple variables in the same boxplot and have the x-axis display the variable names instead of 1 2 3 4.
In other words I want 1 to be Hi_24h, 2 = Hi_mo, etc.
boxplot(project$Hi_24h, project$Hi_mo, project$Lo_24h, project$Lo_mo)

Try:
boxplot(project, names=names(project))
if you do not want all of your columns and would like to select them manually then create a vector:
mynames<-c("Hi_24h", "Hi_mo", "Lo_24h", "Lo_mo")
boxplot(project$Hi_24h, project$Hi_mo, project$Lo_24h, project$Lo_mo, names=mynames

Related

How to label CCA-Plot with row.names in R

I've been trying to solve the following problem which I am sure is an easy one (I am just not able to find a solution). I am using the package vegan and want to perform a cca that shows the actual row names as labels (instead of the default "sit1", "sit2", ...).
I created a dataframe (ls_Treat1) with cast(), showing plot treatments (AB, DB, DL etc.) as row names and species occurences. The dataframe looks as follows:
species 1
species 2
species 3
AB
0
3
1
DB
1
6
0
DL
3
4
2
I created the data frame with the following code to set the treatments (AB, DB, DL, ...) as row names:
ls_Treat1 <- cast(fungi_ls, Treatment ~ species)
row.names(ls_Treat1)<- ls_Treat1$Treatment
ls_Treat1 <- ls_Treat1[,-1]
When I perform a cca with the following code:
ca <- cca(ls_Treat1)
plot(ca,display="sites")
R puts the default labels "sit1", "sit2", ... into the plot, instead of the actual row names, even though I have performed it this way before and the plots normally showed the right labels. Does this have anything to do with my creating the data frame? I tried to change the treatments (characters) into numbers (integers or factors) but still, the plot won't be labelled with my row names.
Can anyone help me with this?
Thank you very very much!!
The problem is that reshape::cast() does not produce data.frame but something else. It claims to be a data.frame but it is not. We do matrix algebra in cca and therefore we cast input to a matrix which works for standard data.frame, but it does not work with the object you supplied as input. In particular, after you remove the first column in ls_Treat1 <- ls_Treat1[,-1], you also remove the attributes that allow preserving names – it would have worked without removing this column (if reshape package was still loaded). It seems that upgrading to reshape2 package and using reshape2::acast() can be a solution.

How to plot multiple UMAPs in a specified grid such as 6 x 3?

I am using the new Seurat 3 package to analyze single-cell sequencing data. I have merged 18 Seurat Objects and have saved the individual identifiers in the meta.data. When plotting out the 18 individual UMAPs using the split.by argument in the DimPlot function, it returns a plot in alphabetical order. It also plots 5 UMAPs on the first three rows and 3 on the last. I would like to plot a 6 by 3 grid and be able to order the UMAPs not alphabetically. Below is the command I used.
DimPlot(object = object, reduction = "umap", split.by = "orig.ident")
If you don't want to go through ggplot2, there is an option inside DimPlot():
library(Seurat)
pbmc_small = FindClusters(pbmc_small,resolution=1.2)
DimPlot(pbmc_small, split.by = "ident")
In this case we have 6 facets:
If we want 2 rows, say with an order 0,5,1,3,2,4 :
DimPlot(pbmc_small, split.by = "ident",ncol=3,order=rev(c(0,5,1,3,2,4)))

Plot a Bar Chart based on Row Names

I am trying to plot a dataframe as follows:
A 1
C 5
B 4
Z 10
M 7
and would it to show the data in the order (i.e. first column in the bar chart is A, second is C, third is B.
I have:
ggplot(pc,aes(x=Let,y=Count))+geom_bar(stat="identity")
And it plots it with the order of the Let column.
df<-data.frame(c('A','C','B','Z','M'),c(1,5,4,10,7))
One way is to convert Let column to factor in the order you want to see them and then use ggplot command.
library(tidyverse)
df$Let <- factor(df$Let, levels = df$Let)
ggplot(df,aes(x=Let,y=Count))+geom_bar(stat="identity")
data
df<-data.frame(Let = c('A','C','B','Z','M'),Count = c(1,5,4,10,7))

r- hist.default, 'x' must be numeric

Just picking up R and I have the following question:
Say I have the following data.frame:
v1 v2 v3
3 16 a
44 457 d
5 23 d
34 122 c
12 222 a
...and so on
I would like to create a histogram or barchart for this in R, but instead of having the x-axis be one of the numeric values, I would like a count by v3. (2 a, 1 c, 2 d...etc.)
If I do hist(dataFrame$v3), I get the error that 'x 'must be numeric.
Why can't it count the instances of each different string like it can for the other columns?
What would be the simplest code for this?
OK. First of all, you should know exactly what a histogram is. It is not a plot of counts. It is a visualization for continuous variables that estimates the underlying probability density function. So do not try to use hist on categorical data. (That's why hist tells you that the value you pass must be numeric.)
If you just want counts of discrete values, that's just a basic bar plot. You can calculate counts of values in R for discrete data using table and then plot that with the basic barplot() command.
barplot(table(dataFrame$v3))
If you want to require a minimum number of observations, try
tbl<-table(dataFrame$v3)
atleast <- function(i) {function(x) x>=i}
barplot(Filter(atleast(10), tbl))

Simple line plot using R ggplot2

I have data as follows in .csv format as I am new to ggplot2 graphs I am not able to do this
T L
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719 2
214.3326667 2
134.4513333 3
169.309 8
161.1313333 4
I tried to plot a line graph using following graph
data<-read.csv("sample.csv",head=TRUE,sep=",")
ggplot(data,aes(T,L))+geom_line()]
but I got following image it is not I want
I want following image as follows
Can anybody help me?
You want to use a variable for the x-axis that has lots of duplicated values and expect the software to guess that the order you want those points plotted is given by the order they appear in the data set. This also means the values of the variable for the x-axis no longer correspond to the actual coordinates in the coordinate system you're plotting in, i.e., you want to map a value of "L=1" to different locations on the x-axis depending on where it appears in your data.
This type of fairly non-sensical thing does not work in ggplot2 out of the box. You have to define a separate variable that has a proper mapping to values on the x-axis ("id" in the code below) and then overwrite the labels with the values for "L".
The coe below shows you how to do this, but it seems like a different graphical display would probbaly be better suited for this kind of data.
data <- as.data.frame(matrix(scan(text="
141.5453333 1
148.7116667 1
154.7373333 1
228.2396667 1
148.4423333 1
131.3893333 1
139.2673333 1
140.5556667 2
143.719 2
214.3326667 2
134.4513333 3
169.309 8
161.1313333 4
"), ncol=2, byrow=TRUE))
names(data) <- c("T", "L")
data$id <- 1:nrow(data)
ggplot(data,aes(x=id, y=T))+geom_line() + xlab("L") +
scale_x_continuous(breaks=data$id, labels=data$L)
You have an error in your code, try this:
ggplot(data,aes(x=L, y=T))+geom_line()
Default arguments for aes are:
aes(x, y, ...)

Resources