barplot with several bars [duplicate] - r

This question already has answers here:
plotting grouped bar charts in R
(3 answers)
Closed 10 months ago.
i have a large dataset with several columns. now i would like to make a barplot to visualise the results, I will first make a dataset that looks like mine
age <- ("18-30","31-45","60+","46-60", "31-45", "18-30", "60+", "46-60")
gender <- ("M","F","F","F","M","M","F","M")
case <- ("Q1","Q1","Q2","Q2","Q3","Q3","Q4","Q4")
height <- (0,200,310,0,0,175,270,150)
Now i would like to make a barplot with on the x-axis the 4 cases, on the Y-axis the average height, and two different bars for M and F indicating the average height.
it should look like this:
except for using the barplot(), I don't really know how to start or what to do, can anyone help?

You could do like this: Put your vectors into a tibble so that you can easily pass them to your ggplot() call.
age <- c("18-30","31-45","60+","46-60", "31-45", "18-30", "60+", "46-60")
gender <- c("M","F","F","F","M","M","F","M")
case <- c("Q1","Q1","Q2","Q2","Q3","Q3","Q4","Q4")
height <- c(0,200,310,0,0,175,270,150)
data <- tibble(age,gender,case,height)
ggplot(data = data, aes(x = case, y = height, fill = gender)) +
geom_col(position = position_dodge(preserve = "single"))

Related

Adding legend to ggplot curves plotted on the same axis [duplicate]

This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 4 months ago.
I have a graph that I'm trying to add a legend to but I can't find any answers.
Here's what the graph looks like
I made a dataframe containing my x-axis as a colum and several othe columns containing y values that I graphed against x (fixed) in order to get these curves. I want a legend to appear on the side saying column 1, ...column 11 and corresponding to the color of the graph
How do I do this? I feel like I'm missing something obvious
Here's what my code looks like:(sorry for the pic. I keep getting errors that my code is not formatted correctly even though I'm using the code button)
interval is just 2:100 and aaaa etc... is a vector the same length as interval.
As Peter says, you will need to convert your data into "long" format. Here is an example using reshape2::melt:
library(reshape2)
library(ggplot2)
n <- 20
df <- data.frame(x = seq(n))
tmp <- as.data.frame(do.call("cbind", lapply(seq(5), FUN = function(x){rnorm(n)})))
names(tmp) <- paste0("aaaa", letters[1:5])
df <- cbind(df, tmp)
head(df)
df2 <- melt(df, id.vars = "x")
head(df2)
ggplot(data = df2) + aes(x = x, y = value, color = variable) +
geom_point() +
geom_line()

row-wise bar plot in r [duplicate]

This question already has answers here:
Single barplot for each row of dataframe
(2 answers)
Closed 13 days ago.
I picked up r recently and was trying some code for data visualization. For practice, I created a small data frame to plot the data and understand the result.
First I tried plotting a simple vector, like temperature over a week, and function barplot worked like a charm.
later I moved on to plot a simple tabular data of marks of students in 2 subjects as shown below:
stuname sub1 sub2
st1 rocket 95 70
st2 Ash 58 85
I used below to create the dataframe
plotdata=data.frame("stuname"=c("rocket","Ash"),
"sub1"=c(95,58),
"sub2"=c(70,85),
row.names = c("st1","st2"))
I am using below to plot the data
barplot(as.matrix(plotdata[ ,2:3]), xlab = "Stu", ylab = "marks", beside = TRUE)
I think the requirement is basic enough so I have not moved to ggplot yet.
This is what I'm getting:
This is what I was expecting:
I mean, this is how usually we would like to plot, we can keep on adding row data and the plot can keep on increasing and I see one figure to get all the marks for a particular student.
Separate just the numeric values and transpose them so that they will plot in the order you want. Note that if you transpose without separating the numeric values, they may be converted to character.
barplot(height = t(plotdata[c("sub1", "sub2")]),
names.arg = plotdata$stuname,
beside = TRUE)
I would still recommend using ggplot as it takes care of so many things for you
library(reshape2)
library(ggplot2)
#Convert to long format
d = melt(plotdata, id.vars = "stuname")
ggplot(data = d,
mapping = aes(x = stuname, y = value, fill = variable)) +
geom_col(position = position_dodge())

Barplot of groups based on counts

I'm trying to make barplot
Data are in dataframe. In those dataframes I have several column, one named ID and another count.
First I'm trying to make group of this count. In the barplot we should see,count=0,count=1,count=2,count>=3
Some exemple data
data1 <- data.frame(ID="ID_1", count=(rep(seq(0,10,by=1),each=4)))
data2 <- data.frame(ID="ID_2", count=(rep(seq(0,10,by=1),each=4)))
data3 <- data.frame(ID="ID_3", count=(rep(seq(0,10,by=1),each=4)))
Obviously here, barplots of the dataframes will look same
I tried to make this in ggplot (it's not nice at all)
ggplot(data1)+
geom_bar(aes(x = ID, fill = count),position = "fill")+
geom_bar(data=data2,aes(x = ID, fill = count),position = "fill")+
geom_bar(data=data3,aes(x = ID, fill = count),position = "fill")
I got something like that
What I'm trying to do is to have different groups within a barplot, like the proportion of counts 0, proportion of counts 1,2 and proportion of counts greater (and equal) to 3.
I expect something like that
But of course in my example barplots will look same.
Also if you have some suggestion to change Y axis from 1.00 to 100%.
Also One of my problem is that length of my real dataframes are not equal but it should doesn't matter because I try to get the percentage of count group
You need to put all the data in 1 dataframe, in long format. Then cast your counts to factors, and it works.
ggplot(bind_rows(data1, data2, data3)) +
geom_bar(aes(x = ID, fill = as.factor(count)), position = "fill") +
scale_y_continuous(labels=scales::percent) # To get the Y axis in percentage
So I did something to try to create my barplot
data1$var="first"
data2$var="second"
data3$var="third"
data4$var="fourth"
data5$var="fifth"
full_data=rbind(data1,data2,data3,data4,data5)
ggplot(ppgk) +
geom_bar(aes(x = var, fill = as.factor(Count)), position = "fill")+
scale_y_continuous(labels=scales::percent)
So I got something like that :
If Someone have the solution to make different group of counts : count=0,count=1,count=2,count>=3

same bar width in ggplot2? [duplicate]

This question already has answers here:
A way to always dodge a histogram? [duplicate]
(2 answers)
Closed 8 years ago.
In this example:
library(ggplot2)
dat <- data.frame(a=factor(c(1,1,1,2,2,2,3,3,3,4)), b=c("A","B","D","A","B","C","A","B","D",NA), c=c(1,4,3,5,5,1,2,2,8,6))
plot <- ggplot(dat,aes(fill=b,x=a,y=c))
plot + geom_bar(width=.7, position=position_dodge(width=.7), stat = "identity")
factor 4 is wider than the other bars. Is there a way to make them all the same width?
Ideally you should have data for every combination even if it is zero. That means, with 1 in data$a you should have data all the four(A,B,C,D) and so on... try modifying your data frame like this and plot. NA category was referred to as "other" here.
library(ggplot2)
dat <- data.frame(a=factor(c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)),
b=c("A","B","C","D","other","A","B","C","D","other","A","B","C","D","other","A","B","C","D","other"),
c=c(1,4,0,3,0,5,5,1,0,0,2,2,0,8,0,0,0,0,0,6))
plot <- ggplot(dat,aes(fill=b,x=a,y=c))
plot + geom_bar(width=.7, position=position_dodge(width=.7), stat = "identity")
View this dataframe you will know the difference. You will obviously have missing bars corresponding to your data, which dnt look good. But im afraid this might be the only solution.

Plotting multiple columns with ggplot2 [duplicate]

This question already has answers here:
Plot multiple columns on the same graph in R [duplicate]
(4 answers)
Closed 4 years ago.
I need to plot the following dataset in the same graph.
Bin1,Bin2,Bin3,Cat
4,3,5,S
6,4,5,M
3,5,4,M
1,4,5,M
,5, ,M
In each bin, first data point belongs to a different category than the rest. (So I added the Cat column)
I need to plot these as points (different colors for the different categories)
Following lines of code achieve what I need for a single bin
p <- ggplot(data,aes(Bin1,1))
p + geom_point(aes(color=Cat, size=Cat))
How do I do this for the entire dataset ?
Here is a related question?
What if I need to use a bunch of columns to color the points. Color Bin1 points according to Cat1 and so on..
Bin1,Cat1,Bin2,Cat2
4,S,5,S
6,L,5,M
3,M,4,L
1,M,5,L
3,M
How do I do this??
library(reshape2)
library(ggplot2)
ggplot(melt(df, id.vars = "Cat"), aes(value, variable, colour = Cat)) +
geom_point(size = 4)
Just melt the data.frame and plot it.
library(reshape2)
dataM <- melt(data, id.vars = "Cat")
p <- ggplot(dataM, aes(value, variable, colour = Cat, size = Cat) + geom_point()

Resources