Here I am trying to able to plot the graph with the below two lines of code.
ggplot(Melvyl,aes(x=Type.of.Customer)) +
geom_histogram(stat="count")
But I want data labels or the count on each of the category, trying the below code but its not working. can you please help me out!
Thank you
ggplot(Melvyl,aes(x=Type.of.Customer)) +
geom_histogram(stat="count")+ stat_bin(binwidth=1, geom="text", aes(label=..count..), vjust=-1.5)
Instead of using geom_histogram you could go on with geom_bar. To add your labels use geom_text with stat="count".
Using mtcars as example data:
library(ggplot2)
ggplot(mtcars, aes(x=cyl)) +
geom_bar() +
geom_text(aes(label=..count..), stat = "count", vjust=-1.5)
Related
I am trying to make a manual colour scale for my bar graph using plyr to summarize the data and ggplot2 to present the graph.
The data has two variables:
Region (displayed on the X-axis)
Genotype (displayed by the fill)
I have managed to do this already, however, I have not been able to find a way to personalize the colours - it simply gives me two randomly assigned colours.
Could someone please help me figure out what I am missing here?
I have included my code and an image of the graph below. The graph basically has the appearance I want it to, except that I can't personalize the colours.
ggplotdata <- summarySE(data, measurevar="Density", groupvars=c("Genotype", "Region"))
ggplotdata
#Plot the data
ggplotdata$Genotype <- factor(ggplotdata$Genotype, c("WT","KO"))
Mygraph <-ggplot(ggplotdata, aes(x=Region, y=Density, fill=Genotype)) +
geom_bar(position=position_dodge(), stat="identity",
colour="black",
size=.2) +
geom_errorbar(aes(ymin=Density-se, ymax=Density+se),
width=.2,
position=position_dodge(.9)) +
xlab(NULL) +
ylab("Density (cells/mm2)") +
scale_colour_manual(name=NULL,
breaks=c("KO", "WT"),
labels=c("KO", "WT"),
values=c("#FFFFFF", "#3366FF")) +
ggtitle("X") +
scale_y_continuous(breaks=0:17*500) +
theme_minimal()
Mygraph
The answer here was to use scale_fill_manual instead, thank you #dc37
I need to plot the geom_histogram but as the plots overlap, I want to plot it linear instead of bars, i.e, the x=hp, y=count (percent).
Can anybody please help me find how to do it.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp)) +
geom_histogram(binwidth=5)
I have done it by qplot but I need to do it in ggplot and the percent.
qplot(hp, data = mtcars, geom = "freqpoly", binwidth = 10)
Thanks
You can use geom_freqpoly:
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp, y=..count../sum(..count..))) +
geom_freqpoly(binwidth=5) +
scale_y_continuous(labels = percent_format()) +
ylab("Percent")
I am very new to R and struggling with something that I know should be simple. I am able to make one scatterplot but would like to make multiple subplots using each feature in my dataset plotted against the variable: per_gop.
My code for one scatter plot is as follows:
data_US#data %>%
ggplot(aes(x=as.numeric(Hispanic_o), y=as.numeric(per_gop)))+
geom_point(aes(fill=as.numeric(gop_dem), size=as.numeric(total_vote)),colour="#525252",pch=21) +
stat_smooth(method=lm, se=FALSE, size=1, colour="#525252")+
scale_fill_distiller(palette="RdBu", type="div", direction=1, guide="colourbar", limits=c(-1,1))+
theme_bw()+
theme(legend.position="none")+
ggtitle(paste("correlation:",round(cor.test(as.numeric(data_US#data$per_gop),as.numeric(data_US#data$Hispanic_o))$estimate,2)))
I have tried using a gather function for this but I am not sure how to correctly pass it to the code for plotting:
My code so far for multiple subplots is as follows:
data_US#data %>%
gather(c(White_alon,Black_or_A, Asian_alon,Hispanic_o,Foreign_bo,
Veterans_2,Language_o,Homeowners,Median_val,Per_capita,Bachelors_,
Private_no), key = "expl_var", value="la_prop") %>%
ggplot(aes(x=??????, y=per_gop))+
geom_point(aes(fill=gop_dem, size=total_vote),colour="#525252",pch=21) +
stat_smooth(method=lm, se=FALSE, size=1, colour="#525252")+
scale_fill_distiller("BrBG", type="div", direction=1, guide="colourbar", limits=c(-1,1))+
facet_wrap(~expl_var, scales="free")+
theme_bw()+
theme(legend.position="none")+ggtitle(paste("correlation:",round(cor.test(data_US#data$per_gop,data_US#data$Persons_65)$estimate,2)))
This is the style of output I am trying to create, just without the repeating variable:
I would be very grateful if someone could point me in the right direction... Thank you!
Here's a reproducible example using the mtcars dataset. Should be possible to apply same form to your data, but hard to know for sure without seeing an example of that data.
library(tidyverse)
mtcars %>%
gather(expl_var, la_prop, -mpg) %>%
ggplot(aes(la_prop, mpg)) +
geom_point(colour="#525252",pch=21) +
stat_smooth(method=lm, se=FALSE, size=1, colour="#525252")+
facet_wrap(~expl_var, scales = "free") +
theme_bw()+
theme(legend.position="none")
data:
df<-data.frame(grp=letters[1:4],perc=runif(4))
First option:
First, create a second dataset that contains zeros for each group
df2<-rbind(df,data.frame(grp=df[,1],perc=c(0,0,0,0)))
Then plot with geom_points and geom_line:
ggplot(df,aes(y=perc,x=grp))+
geom_point()+
geom_line(data=df2, aes(y=perc, x=grp))+
coord_flip()
Which looks just fine. Just too much extra work to create a second dataset.
The other option is using geom_bar and making the width tiny:
ggplot(df,aes(y=perc,x=grp))+
geom_point()+
geom_bar(stat="identity",width=.01)+
coord_flip()
But this is also weird, and when I save to .pdf, not all of the bars are the same width.
There clearly has to be an easier way to do this, any suggestions?
Use geom_segment with fixed yend = 0. You'll also need expand_limits to adjust the plotting area:
ggplot(df, aes(y=perc, x=grp)) +
geom_point() +
geom_segment(aes(xend=grp), yend=0) +
expand_limits(y=0) +
coord_flip()
I am trying to plot side by side the following datasets
dataset1=data.frame(obs=runif(20,min=1,max=10))
dataset2=data.frame(obs=runif(20,min=1,max=20))
dataset3=data.frame(obs=runif(20,min=5,max=10))
dataset4=data.frame(obs=runif(20,min=8,max=10))
I've tried to add the option position="dodge" for geom_histogram with no luck. How can I change the following code to plot the histograms columns side by side without overlap ??
ggplot(data = dataset1,aes_string(x = "obs",fill="dataset")) +
geom_histogram(binwidth = 1,colour="black", fill="blue")+
geom_histogram(data=dataset2, aes_string(x="obs"),binwidth = 1,colour="black",fill="green")+
geom_histogram(data=dataset3, aes_string(x="obs"),binwidth = 1,colour="black",fill="red")+
geom_histogram(data=dataset4, aes_string(x="obs"),binwidth = 1,colour="black",fill="orange")
ggplot2 works best with "long" data, where all the data is in a single data frame and different groups are described by other variables in the data frame. To that end
DF <- rbind(data.frame(fill="blue", obs=dataset1$obs),
data.frame(fill="green", obs=dataset2$obs),
data.frame(fill="red", obs=dataset3$obs),
data.frame(fill="orange", obs=dataset3$obs))
where I've added a fill column which has the values that you used in your histograms. Given that, the plot can be made with:
ggplot(DF, aes(x=obs, fill=fill)) +
geom_histogram(binwidth=1, colour="black", position="dodge") +
scale_fill_identity()
where position="dodge" now works.
You don't have to use the literal fill color as the distinction. Here is a version that uses the dataset number instead.
DF <- rbind(data.frame(dataset=1, obs=dataset1$obs),
data.frame(dataset=2, obs=dataset2$obs),
data.frame(dataset=3, obs=dataset3$obs),
data.frame(dataset=4, obs=dataset3$obs))
DF$dataset <- as.factor(DF$dataset)
ggplot(DF, aes(x=obs, fill=dataset)) +
geom_histogram(binwidth=1, colour="black", position="dodge") +
scale_fill_manual(breaks=1:4, values=c("blue","green","red","orange"))
This is the same except for the legend.