Barplot/barchart in R [duplicate] - r

This question already has answers here:
How to create grouped barplot with R [duplicate]
(1 answer)
plotting grouped bar charts in R
(3 answers)
Closed 3 years ago.
I have a data table with two small columns.
I want to do a pairwise comparison between the values. The first column is results of one test and the second of another test. So I want a barplot where the first pair of bars show value [1,1] and next to it [1,2] then [1,2] besides [2,2] and so on.
I have 20 values (10 in each from 10 instances) and want 20 bars in one plot. I have no category variable but I want to preserve the order in which they appear in the column (each result corrosponds to an instance). Hence 20 values represented by 20 bars.
Hope you can help.
Edit: Sry for the bad explanation.

Based on your vague question I think this is what you want. Here is a quick example you can use to get your results:
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
xlab="Number of Gears", col=c("darkblue","red"),
legend = rownames(counts), beside=TRUE)
Source: http://www.statmethods.net/graphs/bar.html

Related

plot 3 continuous variables against each other in one plot [duplicate]

This question already has answers here:
Plot each column against each column
(2 answers)
Plot all pairs of variables in R data frame based on column type [duplicate]
(1 answer)
Closed 17 days ago.
I have the output of 3 different algorithms as a continuous vector. Instead of comparing their correlation 1 by 1, I would like to plot them all simuntaionusly in the same plot, but in different panels. The dataframe looks like this (but contains >10k ids):
df <- data.frame(id=1:5,
feature1=runif(5),
feature2=runif(5,min = 3,max=5),
feature3=runif(5, min = 5,max=8))
Ideally, the resulting plot should looks something like this:
I am fairly sure that there is some simple tidyr function, which expands my dataframe in such a way that I can simply use ggplot2 in combination with facet_grid, but I searched and coudn't find anything..
Any help is much appreciated!

R: How do I make a single variable's values into 3 smaller groups (with limits like <5, 5-10, >10)? [duplicate]

This question already has answers here:
Categorize numeric variable into group/ bins/ breaks
(4 answers)
Closed 2 years ago.
I am struggling to make a barplot with two variables in R. One variable has data ranging from 0-90, and I need to split it up into 3 groups-- the data that is <5, 5-10, and >10. So that there are only 3 bars in the plot instead of 90. Here is the code I have tried to use but I can't figure out how to get this to work. The problem is in the use of the <,>, and - signs.
First I created a new variable
SVLivedPlot <- SDreal2$SVLived
And then I am trying to group all the numbers that are under 5 to be the value of 1, 5-10 to be the value of 2, and greater than 10 to be 3.
SVLivedPlot[SDreal2$SVLived == c(<5)] <- 1
SVLivedPlot[SDreal2$SVLived == c(5-10)] <- 2
SVLivedPlot[SDreal2$SVLived == c(>90)] <-3
Once I get those values changed I will use the following code to save that new variable with the correct groupings as the variable I will use in my barplot
DataFrameName$OldVariableName <- NewVariableName
Once I can get this new variable created I know how to put it in the barplot() formula to get the plot. I just need to know how to group those data! Any help would be great! Thank you!:)
We can use cut
SDreal2$NewVar <- with(SDreal2, as.integer(cut(SVLived,
breaks = c(-Inf, 5, 10, 90))))

How to remove value from a plot on R [duplicate]

This question already has answers here:
Boxplot sees values that aren't there
(1 answer)
How to remove ticks and labels of dropped off factors in a box plot
(1 answer)
Closed 5 years ago.
You can see my plot in this link.
I want to remove the first three points from the x axis, so I only display data from "2.31-52k", "1.52-100k", "3.18-31k", "4.<18k", "0.>100k". And not -1.0,-3.1 etc.
I have managed to remove the data to produce this graph, by creating a subset in dAll$income and assigning it to a new data frame with the following code.
new_df = subset(dAll, subset = dAll$income %in% c("2.31-52k", "1.52-100k", "3.18-31k", "4.<18k", "0.>100k"))
However when I plot this, the values remain on the x axis.

Plotting pre-binned data of percent time allotments [duplicate]

This question already has answers here:
population pyramid density plot in r
(5 answers)
Closed 9 years ago.
I have 11 data columns in a single data.frame, 10 of the columns are depth categories and one column contains one of two values (Day or Night).
Each row of data is populated with numeric values representing the percent of time spent in each depth category over a 4-hour period and has either Day or Night assigned to it in the Day_Night column indicating whether that 4-hour period was during the day or the night.
I would like to plot this data in a back to back histogram where each depth bin is listed vertically along the plot and values for the day are plotted in one histogram facing one way, while the values for the night are plotted facing the other way. I can't post a picture since I am new but to see what I mean just look at Figure 5 on page 8 of this publication.
EDIT: Here's the picture:
Thanks for the help!
You can use plotrix for example:
set.seed (123)
mm <- matrix (sample(seq(0,330,10),2000,rep=TRUE),ncol=2 )
ll <- lapply(as.data.frame(mm),
function(x)as.data.frame(table(cut(x,seq(0,330,10)))))
library(plotrix)
par(mar=pyramid.plot(ll$V1$Freq,ll$V2$Freq,
labels =ll$V1$Var1,
top.labels=c("NIGHT","","DAY"),
xlim=c(100,100),
main="Swimming depth",
lxcol="black",rxcol= "white",
gap=0,
do.first="plot_bg('pink')"))

Sorting data on X axis [duplicate]

This question already has answers here:
Reorder levels of a factor without changing order of values
(9 answers)
Closed 9 years ago.
I have created a plot of coverage vs gene. I would like to plot the genes (x axis) from lowest coverage value (y axis) to largest. I used the function "plot" but it automatically sorts the gene names from lowest to highest. How do I arrange them from lowest coverage to highest instead?
Maybe switch the ylim:
plot(1:10, ylim=c(10,1))

Resources