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()
Related
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()
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"))
This question already has answers here:
How can I remove empty factors from ggplot2 facets?
(2 answers)
Closed 4 years ago.
I need to display a faceted bar plot for the count of levels in all the columns of the dataset.
the code,
aas <- sapply(AAA, is.factor)
aas1 <- AAA[,aas]
overview of aas1 dataset
aas2 <- data.frame(t(aas1))
aas2 <- cbind(names = rownames(aas2), aas2)
library(reshape2)
aas3 <- melt(aas2,id = "names")
aas3$variable <- 1
aas3$value <- as.factor(aas3$value)
aas4 <- aggregate(.~ names + value, aas3, sum)
overview of aas4 dataset
ggplot(aas4, aes(x=factor(value), y= variable)) +
geom_bar(stat="identity") + facet_grid(.~names) +
scale_fill_discrete(name="value") + xlab("")
facet bar plot
when i use aas4 dataset for faceted bar plot, each facet is consisting of all levels of "value", but i only need facets with levels corresponding to the variable "names"
Please suggest any better way to plot counts of levels of all factor variables in the dataset.
You need to specify that you want scales="free_x" and, in order not to have the same space for each facet, space="free".
+ facet_grid(.~names, scales="free_x", space="free_x")
This question already has answers here:
Easily add an '(all)' facet to facet_wrap in ggplot2?
(3 answers)
Closed 5 years ago.
It is often the case that we produce facets to decompose the data according to a variable, but that we still would like to see a summary as a stack of the facets. Here is an example:
library(ggplot2)
ggplot(data=iris, aes(x=Sepal.Length,y=Petal.Length)) +
geom_point(aes(color=Species)) +
facet_wrap(~Species, ncol=2)
However, I would also like that one of the facets is the overlay of the 3 facets:
ggplot(data=iris, aes(x=Sepal.Length,y=Petal.Length)) +
geom_point(aes(color=Species))
Is there anyway of doing this easily?
Many thanks,
I wrote the following function to duplicate the dataset and create an extra copy under of the data under variable all.
library(ggplot2)
# Create an additional set of data
CreateAllFacet <- function(df, col){
df$facet <- df[[col]]
temp <- df
temp$facet <- "all"
return(rbind(temp, df))
}
Instead of overwriting the original facet data column, the function creates a new column called facet. The benefit of this is that we can use the original column to specify the aesthetics of the plot point.
df <- CreateAllFacet(iris, "Species")
ggplot(data=df, aes(x=Sepal.Length,y=Petal.Length)) +
geom_point(aes(color=Species)) +
facet_wrap(~facet, ncol=2)
I feel the legend is optional in this case, as it largely duplicates information already available within the plot. It can easily be hidden with the extra line + theme(legend.position = "none")
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 three values, to make three bars for each value of the X-Axis. My data is:
In the X-Axis must be the column labeled as "m" and for each "m" value I need to plot the correspondent "x","y" and "z" value.
I want to use ggplot2 and I need something like this:
I created my own dataset to demonstrate how to do it:
Data:
x <- runif(12,1,1.5)
y <- runif(12,1,1.5)
z <- runif(12,1,1.5)
m <- letters[1:12]
df <- data.frame(x,y,z,m)
Solution:
#first of all you need to melt your data.frame
library(reshape2)
#when you melt essentially you create only one column with the value
#and one column with the variable i.e. your x,y,z
df <- melt(df, id.vars='m')
#ggplot it. x axis will be m, y will be the value and fill will be
#essentially your x,y,z
library(ggplot2)
ggplot(df, aes(x=m, y=value, fill=variable)) + geom_bar(stat='identity')
Output:
If you want the bars one next to the other you need to specify the dodge position at geom_bar i.e.:
ggplot(df, aes(x=m, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge')