I created this bar chart using ggplot. I had to create my own count function and called it 'a,' but now the label for that axis just has an a and nothing else... How do I fix it?
a <- count(df, glass)
gl <- ggplot(df, aes(x=glass, y="a", fill=glass)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
xlab("Glass type") +
ylab("Count") +
coord_flip() +
theme_minimal() +
theme(legend.position = "none")
gl
Here is my graph
The default stat value for geom_bar is “count”, which means that geom_bar() uses stat_count() to count the rows of each x value, or glass in this case. Using stat="identity" will override the default geom_bar() stat and require that you provide the y values in order for aggregation to occur. I don't believe you are trying to do this.
Try the following and see if it is what you were looking for:
gl <- ggplot(df, aes(x=glass, fill=glass)) +
geom_bar() +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
xlab("Glass type") +
ylab("Count") +
coord_flip() +
theme_minimal() +
theme(legend.position = "none")
Related
I have the following ggplot2 codes running in R. I need to tweak the codes so that the bars of each FY value are next to each other rather than being stacked.
My codes stand as follows:
p1 <- ggplot(dff3, aes(x=Gender, fill=FY)) + ggtitle("Gender") +
xlab("Gender") +
geom_bar(aes(y = 100*(..count..)/sum(..count..)), width = 0.5) +
ylab("Percentage") +
coord_flip() +
theme_minimal() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
p1
The plot looks like this:
rently like this:
You can use position_dodge() within geom_bar(). Here is an example using mtcars dataset:
library(tidyverse)
ggplot(mtcars, aes(x=factor(am), fill=factor(vs))) +
ggtitle("Gender") +
xlab("Gender") +
geom_bar(aes(y = 100*(..count..)/sum(..count..)), width = 0.5, position = position_dodge()) +
ylab("Percentage") +
coord_flip() +
theme_minimal() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
Is there a way to add a line for specific factor levels in ggplot?
this simple example could provide a base to explain what I'm trying to say. In this case I'd like to avoid plotting the last level.
ggplot(BOD, aes(x=factor(Time), y=demand, group=1)) + geom_line() + geom_point()
You can just simply create a new variable with an NA-value for Time == 7:
BOD$demand2[BOD$Time<7] <- BOD$demand[BOD$Time<7]
and then plot:
ggplot(BOD, aes(x=factor(Time), y=demand2, group=1)) +
geom_line() +
geom_point() +
theme_classic()
You could also do it on the fly by utilizing the functionality of the data.table-package:
library(data.table)
ggplot(data = as.data.table(BOD)[Time==7, demand := NA],
aes(x=factor(Time), y=demand, group=1)) +
geom_line() +
geom_point() +
theme_classic()
To answer your comment, you could include the point at 7 as follows:
ggplot(BOD, aes(x=factor(Time), y=demand2, group=1)) +
geom_line() +
geom_point(aes(x=factor(Time), y=demand)) +
theme_classic()
I'm working on some flattening of overlapping ranges and would like to visualize the initial data (overlapping) and the resulting set (flattened) the following way:
Initial data:
Resulting set:
Is such possible with R and, for example, ggplot2?
read.table(header=TRUE, sep=",", text="color,start,end
red,12.5,13.8
blue,0.0,5.4
green,2.0,12.0
yellow,3.5,6.7
orange,6.7,10.0", stringsAsFactors=FALSE) -> df
library(ggplot2)
df$color <- factor(df$color, levels=rev(df$color))
ggplot(df) +
geom_segment(aes(x=start, xend=end, y=color, yend=color, color=color), size=10) +
scale_x_continuous(expand=c(0,0)) +
scale_color_identity() +
labs(x=NULL, y=NULL) +
theme_minimal() +
theme(panel.grid=element_blank()) +
theme(axis.text.x=element_blank()) +
theme(plot.margin=margin(30,30,30,30))
There are other posts on SO that show how to get the y labels like you have shown (we can't do all the work for you ;-)
The answer to the second part of the question can be using #hrbrmstr 's great answer for the first part. We can use overplotting to our advantage and simply set the y coordinates for the segments to a fixed value (for example 1, which where "red" is):
p <- ggplot(df) +
geom_segment(aes(x=start, xend=end, color=color),
y=1, yend=1, size=10) +
scale_x_continuous(expand=c(0,0)) + scale_color_identity() +
labs(x=NULL, y=NULL) +
theme_minimal() +theme(panel.grid=element_blank()) +
theme(axis.text.x=element_blank()) +
theme(plot.margin=margin(30,30,30,30))
print(p)
I would like to colour my boxplot variables differently. I looked here and tried the following but the plot boxes are all the standard white colour (i have 6 factors in Type). What should i change?
library(ggplot2)
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot() +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))
Also you can just change from geom_boxplot() to geom_boxplot(aes(fill=Type)) in you original codes.
ex:
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot(aes(fill=Type)) +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))
What has to change is
geom_boxplot() +
to
geom_boxplot(fill = c("white","white","white","red","blue","white")) +
and remove
scale_fill_manual(values = c("white","white","white","red","blue","white"))
Using the this code gives the plot printed below. As you can see the percentages are printed on the border of the bars. I would like to have them above the bars. Is there a way to achieve this?
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..),label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")
Just add an arbitrary value to y.
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..) + 10,label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")
Or, as per Heroka's comment, use vjust, which is a better solution
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +
geom_text(aes(y = (..count..),
label = scales::percent((..count..)/sum(..count..))),
stat="bin",
colour="darkgreen", vjust = -0.5) +
theme(legend.position="none")
But as this makes things quite cramped at the top you might want to add + expand_limits(y = c(0, 60)) to give you a bit more space for the labels.