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.
Related
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:
Using ggplot2, can I insert a break in the axis?
(10 answers)
Closed 2 years ago.
I want to make a plot with not equally spaced axis. Here is my data.
group <- c("group1","group2","group3","group4","group5","group6")
value <- c(520,550,13,15,30,20)
df <- cbind.data.frame(group,value)
I want to make a plot like this.
idealplot
I tried to make it using ggplot, like this.
ggplot(df,aes(x=factor(group),y=value))+geom_bar(stat="identity")
This code makes a plot with equally spaced axis which I do not want. This plot is unable to compare group 3,4,5 and 6. If you know how to make the ideal plot, please let me know.
undesiredplot
I don't think I've ever seen a discontinuous axis in ggplot2.
With points instead of bars, you can fake a discontinuous axis by using facets:
ggplot(df,aes(x=factor(group),y=value)) +
facet_grid(value < 100 ~., scales='free_y') +
geom_point(stat="identity") +
theme(strip.text = element_blank())
Other alternatives to visualizing data with large differences in values is to log-transform or sqrt-transform the Y axis, which effectively stretches out small values and compresses large values.
ggplot(df,aes(x=factor(group),y=value)) +
geom_bar(stat="identity") +
scale_y_log10() +
annotation_logticks(base=10, sides='lr')
This question already has answers here:
Plotting two variables as lines using ggplot2 on the same graph
(5 answers)
Closed 5 years ago.
This is my first time using ggplot2. I have a table of 3 columns and I want to plot frequency distribution of all three columns in one figure. I have only used hist() before so I am a little lost on this ggplot2. Here is an example of my table. Tab separated table with 3 columns A,B,C headers.
A B C
1.38502 1.38502 -nan
0.637291 0.753084 1.55556
0.0155242 0.0164394 -nan
3.29355 1.15757 -nan
1.00254 1.10108 0.132039
0.0155424 0.0155424 nan
0.760261 0.681639 0.298851
1.21365 1.21365 -nan
1.216 1.22541 -nan
0.61317 0.738528 0.585657
0.618276 0.940312 0.820591
1.96779 1.31051 1.58609
0.725413 2.29621 1.78989
0.684681 0.67331 0.290221
I have used the following code by looking up similar posts but I end up with error.
library(ggplot2)
dnds <- read.table('dNdS_plotfile', header =TRUE)
ggplot(data=dnds, melt(dnds), aes_(value, fill = L1))+
geom_histogram()
ERROR:No id variables; using all as measure variables
Error: Mapping should be created with aes() or aes_().
I am really lost on how to solve this error. I want one figure with three different colored histograms that do not overlap in my final figure. Please help me achieve this. Thank you.
This should accomplish what you're looking for. I like to load the package tidyverse, which loads a bunch of helpful packages like ggplot2 and dplyr.
In geom_histogram(), you can specify the bindwidth of the histograms with the argument binwidth() or the number of bins with bins(). If you also want the bars to not be stacked you can use the argument position = "dodge".
See the documentation here: http://ggplot2.tidyverse.org/reference/geom_histogram.html
library(tidyverse)
data <- read.table("YOUR_DATA", header = T)
graph <- data %>%
gather(category, value)
ggplot(graph, aes(x = value, fill = category)) +
geom_histogram(binwidth = 0.5, color = "black")
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 6 years ago.
I have a data frame df1 and want to draw a barplot of AccountExecutive and their corresponding ClearRate where the bars are arranged so that it is decreasing from left to right.
I tried this code but the resulting graph still reflects AccountExecutive order as it appears in df1
ggplot(arrange(df1, -ClearRate), aes(x = AccountExecutive, y = ClearRate)) +
geom_bar(stat="identity")
Can anyone help me correcting this code?
NOTE: Not a duplicate of the previous question because that one asks for an arbitrary positioning of the x axis labels. This question asks how to sort x-axis labels considering their y-axis values.
Try this one the code below should reorder AE according to clearance rate
ggplot(df1,aes(x=reorder(AccountExecutive,-ClearRate),y=ClearRate))+geom_bar(stat"identity")
here is the more about reorder function
Reorder bars in geom_bar ggplot2
This question already has answers here:
Create a histogram for weighted values
(3 answers)
Closed 6 years ago.
This is the head of a data set containing 101302 observations. It is listing vehicle weight, and the number of registrations. I want to plot this as a histogram in R.
r mkg
3 1495
1 1447
1 1401
1 2405
1 2635
2 2515
I need to plot a histogram of the mkg variable, but I need to allow for the number of registrations. I'm not sure how to approach this. I'm sorry, I'm sure this is basic but I've looked all day for an answer and haven't found one that works.
Using ggplot2 package, you can try something like this:
library(ggplot2)
ggplot(df, aes(x = mkg)) + geom_histogram() + facet_wrap(~r)
It will make as many plots as there are unique values in column r.
If you want to plot all histograms on the same plot, you can try this:
library(ggplot2)
ggplot(df, aes(x = mkg, fill = r)) + geom_histogram()