Stacked bar plot in R - r

I have a csv file like below, I want to make a stacked bar plot that x-axis is link column and and y-axis shows the frequency and each bar is grouped based on Freq_E and Freq_S. when I read the csv and give it to barplot it doesn't work. I searched a lot but all examples data is in form of contingency table. I donno what should I do...
link Freq_E Freq_S
1 tube.com 214 214
2 list.net 120 120
3 vector.com 119 118
4 4cdn.co 95 96

"It doesn't work" is not an error message in R that I'm familiar with, but I'm guessing your problem is that you are trying to use barplot on a data.frame while you should be using a matrix or a vector.
Assuming your data.frame is called "df" (as defined at the start of Codoremifa's answer), you can try the following:
x <- as.matrix(df[-1]) ## Drop the first column since it's a character vector
rownames(x) <- df[, 1] ## Add the first column back in as the rownames
barplot(t(x)) ## Transpose the new matrix and plot it

You should look at the excellent ggplot2 library, try this code snippet for your example -
df <- read.table(textConnection(
'link Freq_E Freq_S
tube.com 214 214
list.net 120 120
vector.com 119 118
4cdn.co 95 96'), header = TRUE)
library(ggplot2)
library(reshape2)
df <- melt(df, id = 'link')
ggplot(
data = df,
aes(
y = value,
x = link,
group = variable,
shape = variable,
fill = variable
)
) +
geom_bar(stat = "identity")

Related

problem display grouped boxplot with ggplot2

I have problem with my grouped boxplot, they are not display as boxplot, i can't understand why.
This is what i did:
library(ggplot2)
require(reshape2)
args = commandArgs(trailingOnly=TRUE)
data <- read.table(args[1], header=T, sep="\t")
data.m <- melt(data, id.var = "dijarn")
ggplot(data = data.m, aes(x=dijarn, y=value)) + geom_boxplot(aes(fill=variable))
exemple of my data :
> head(data.m)
dijarn variable value
1 dijarn043 ATP5PB 2230.746
2 dijarn044 ATP5PB 2501.788
3 dijarn045 ATP5PB 2067.263
4 dijarn046 ATP5PB 4060.777
5 dijarn047 ATP5PB 3075.087
6 dijarn048 ATP5PB 2892.501
i have 37 dijarn and here 5 variable.
then it's a lot but i think ggplot2 can handle it ?
i tried to chance size of my image but i didn't change anything. did i forget some option ?
thanks for your help
Are you trying to create 5 boxplots one for each level of the variable or 37 boxplots one for each level of dijarn? You can't create a boxplot of one observation. Boxplots reflect the IQR (quartiles of a range)
try factoring the variable you want a boxplot on
data$dijarn <- as.factor(data$dijarn)

Grouped Bar Plot Extra Variable in R

I have the following data frame in R:
> data <- data.frame(tbi_military[0:4])
> data
Severity Active Guard Reserve
1 Penetrating 189 33 12
2 Severe 102 26 11
3 Moderate 709 177 63
4 Mild 5896 1332 541
5 Not Classifiable 122 29 12
And when I do barplot(as.matrix(data)) I get the following output:
Barplot Image
Is there a way for me to get rid of the severity on the x-axis to only have Active, Guard, Reserve? Thanks
one option is to send only the data you want to plot to the plotting function. In this case you want all columns from the second to the last (number four) so a small adjustment to your function call does the job:
barplot(as.matrix(data[, 2:4]))
A solution within the tidyverse (dplyr, tidyr and ggplot2) would be this:
library(dplyr)
library(tidyr)
library(ggplot2)
data %>%
# get data in tidy format to be able to use ggplot2 efciently
tidyr::pivot_longer(-Severity, names_to = "Type", values_to = "Value") %>%
# set up the plot by assigning variable to plot
ggplot2::ggplot(aes(Type, Value, fill = Severity)) +
# put out a bar chart with stat parameter set for stacked barchart
ggplot2::geom_bar(stat = "identity")

How to change bar position without affecting the results?

I'm new to R and im trying to change the position of a bar on a bar chart but my results have changed too. Here is the chart : Chart of age
when I use the code :
positions <- c("Moins de 18 ans","18 a 22 ans", "23 a 27 ans", "33 a 37 ans","38 ans et plus")
p + theme_classic() + scale_x_discrete(limits = positions)
This is the results I have:
Chart of age 2
and the message :
Warning messages:
1: Removed 86 rows containing non-finite values (stat_count).
2: Removed 86 rows containing non-finite values (stat_count).
I don't know what to do with this. Someone help me please!
Since you haven't provided the data, I can show how to rearrange bars using dummy data. To sort bar graph, essentially you need to sort data on variables you are using as x-axis in plot.
vec = c(rep("a", 30), rep("b", 20), rep("c", 10))
df = as.data.frame(table(vec)) # Create dummy data frame
Dataframe df looks like this -
vec Freq
1 a 30
2 b 20
3 c 10
The plot will be -
df %>%
ggplot(aes(x = vec, y = Freq)) +
geom_bar(stat = "identity") # default plot
Now, I want bars in order b,a,c. All I need to do is sort my dataframe in the same order -
df$vec = factor(df$vec, levels = c("b", "a", "c")) # assign levels in order you want to see the bar-plot
df = df[order(df$vec),] # sort dataframe on your x-variable
df %>%
ggplot(aes(x = vec, y = Freq)) +
geom_bar(stat = "identity") # barplot will be sorted on levels of factor now
The output of above code is -
I haven't done rest of the formatting, but from your graphs, you are good with that. By following these steps, your data shouldn't change when reordering the bars. If you can share your data, I can better understand if it solves your problem.

Generating a histogram and density plot from binned data

I've binned some data and currently have a dataframe that consists of two columns, one that specifies a bin range and another that specifies the frequency like this:-
> head(data)
binRange Frequency
1 (0,0.025] 88
2 (0.025,0.05] 72
3 (0.05,0.075] 92
4 (0.075,0.1] 38
5 (0.1,0.125] 20
6 (0.125,0.15] 16
I want to plot a histogram and density plot using this but I can't seem to find a way of doing so without having to generate new bins etc. Using this solution here I tried to do the following:-
p <- ggplot(data, aes(x= binRange, y=Frequency)) + geom_histogram(stat="identity")
but it crashes. Anyone know of how to deal with this?
Thank you
the problem is that ggplot doesnt understand the data the way you input it, you need to reshape it like so (I am not a regex-master, so surely there are better ways to do is):
df <- read.table(header = TRUE, text = "
binRange Frequency
1 (0,0.025] 88
2 (0.025,0.05] 72
3 (0.05,0.075] 92
4 (0.075,0.1] 38
5 (0.1,0.125] 20
6 (0.125,0.15] 16")
library(stringr)
library(splitstackshape)
library(ggplot2)
# extract the numbers out,
df$binRange <- str_extract(df$binRange, "[0-9].*[0-9]+")
# split the data using the , into to columns:
# one for the start-point and one for the end-point
df <- cSplit(df, "binRange")
# plot it, you actually dont need the second column
ggplot(df, aes(x = binRange_1, y = Frequency, width = 0.025)) +
geom_bar(stat = "identity", breaks=seq(0,0.125, by=0.025))
or if you don't want the data to be interpreted numerically, you can just simply do the following:
df <- read.table(header = TRUE, text = "
binRange Frequency
1 (0,0.025] 88
2 (0.025,0.05] 72
3 (0.05,0.075] 92
4 (0.075,0.1] 38
5 (0.1,0.125] 20
6 (0.125,0.15] 16")
library(ggplot2)
ggplot(df, aes(x = binRange, y = Frequency)) + geom_bar(stat = "identity")
you won't be able to plot a density-plot with your data, given its not continous but rather categorical, thats why I actually prefer the second way of showing it,
You can try
library(ggplot2)
ggplot(df, aes(x = binRange, y = Frequency)) + geom_col()

working with 3 columns of data in ggplot2: x, y1, and y2 into a stacked bar plot

I have 3 column data. The first column, depth, should be on the x axis. The other two columns are nr and r. I need to plot the data in a stacked barplot with A on the bottom and B on the top of nr. The data is very large (ie. the read depth goes from 0 to 1022), so I can't type everything out specifically in r or on here. Here's an example of what the data would look like:
Depth r nr
6 2395 2904
8 0 3095
9 2689 0
12 3894 3578
15 5 4739
the r and the nr have to be on the y axis, and the depth has to be on the x axis. I've tried everything I can think of and am unable to get a 'height' to use or to just get the basic equation.
Work in long format
#using reshape2::melt
library(reshape2)
# assuming your original data.frame is called `D`
longD <- melt(D, id.var = 1)
ggplot(longD, aes(x = Depth, y = value, colour = variable, fill = variable)) +
geom_bar(stat = 'identity')
Using barchart from lattice you can deal with wide format :
library(lattice)
barchart(r+nr~factor(Depth),data=dt,stack=TRUE,auto.key=TRUE)
equivalent to this , using long format from #mnel answer:
barchart(value~factor(Depth),data=longD,
groups=variable,stack=TRUE,auto.key=TRUE)
Just to show base R graphics can match it as well, and assuming your data.frame is called dat:
barplot(
t(dat)[2:3,],
names.arg=t(dat)[1,],
space=c(0,diff(t(dat)[1,])),
axis.lty=1
)

Resources