Thi is my data:
x <- c("22-01-16","26-01-16","28-01-16","01-02-16","05-02-16","16-02-16","17-03-16","18-03-16","04-04-16","05-04-16","06-04-16","08-04-16")
y <- c(97.14,75,54.44,70.45,110.56,66.3,178.76,171.90,419.41,424,518.63,242.17)
z <- c("ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP")
So I make the dataframe
Datos <- data.frame(x)
Datos$Caudal <- y
Datos$Tipo <- z
Datos$Fecha <- as.Date(Datos$x, "%d-%m-%y")
and plot using ggplot2
Serie_Caudal <-
ggplot(Datos, aes(Fecha, Caudal)) +
geom_line(size=1, colour="red") +
geom_point(shape=23,size=1, colour="blue",fill = "blue") +
scale_x_date(date_breaks = "1 week",labels = date_format("%d/%b"))+
xlab("Fecha") + ylab(bquote('Caudal ('*m^3~s^-1*')')) +
ggtitle("Caudales Diarios (01-06/2016)")
Serie_Caudal
I try to plot a legend but i can´t the way, i try use Melt but my data change in a way i can´t plot. Also try scale_fill_manual but the legend don´t show up. I want to know if there is a way to put a legend manualy.
The legend must show a blue point and ADCP
This shows only a blue dot.
ggplot(aes(Fecha, Caudal, colour = "ADCP"), data = Datos) +
geom_point() +
geom_point(shape=23,size=1,color="blue",fill = "blue") +
scale_color_manual(values = c("ADCP"="blue"),name = "") +
geom_line(color="red", size=1) +
scale_x_date(date_breaks = "1 week",labels = date_format("%d/%b")) +
xlab("Fecha") + ylab(bquote('Caudal ('*m^3~s^-1*')')) +
ggtitle("Caudales Diarios (01-06/2016)")
Related
I want to use geom_segment to make a line intercept the y-axis, the problem I am experiencing becomes readily apparent. Starting off with sample data:
dat <- data.frame(Yaxis = c(100,200,300,400,500,600,700,800), Year = c(2012,2013,2014,2015,2016,2017,2018,2019))
p <- ggplot(data=dat, aes(x=Year)) + geom_line(aes(y=Yaxis))
and adding a geom_segment layer produces:
p + geom_segment(aes(x=0,xend=2012,y=75,yend=100))
my attempts to amend this haven't worked out so far:
p + geom_segment(aes(x=0,xend=2012,y=75,yend=100)) + scale_x_continuous(expand= c(0,0), breaks = c(2012,2013,2014,2015,2016,2017,2018,2019))
For reference, here is a crude drawing of what I intend the graph to look like:
Thanks to #Sathish 's answer, I was able to create the graph I intended using
ggplot(data=dat, aes(x=as.Date(paste(Year,'-01-01', sep=""), "%Y-%m-%d"), y=Yaxis)) +
geom_line() +
geom_segment(aes(
x=as.Date("2011-01-01", "%Y-%m-%d"),
xend=as.Date("2012-01-01", "%Y-%m-%d"),
y=75,
yend=100), color = "red") +
scale_x_date(expand = c(0,0))
Convert Year column to Date class.
library('ggplot2')
ggplot(data=dat, aes(x=as.Date(paste(Year, '-01-01', sep = ""), "%Y-%m-%d"), y=Yaxis)) +
geom_line() +
geom_segment(aes(x=as.Date("2012-01-01", "%Y-%m-%d"),xend=as.Date("2020-01-01", "%Y-%m-%d"),y=75,yend=100), color = "red") +
xlab(label = "Year")
How can I add annotation in ggplot like this? I need to add the text (CV, Network), the bars, and also those stars.
I hope you can transfer this example to your dataset:
ggplot() + geom_point(aes(x = 1:10, y = 1:10)) +
geom_segment(aes(x=0,y=11,xend=5,yend=11)) + geom_segment(aes(x=0,y=11,xend=0,yend=10.5)) + geom_segment(aes(x=5,y=11,xend=5,yend=10.5)) + ##bracket 1
geom_segment(aes(x=5.5,y=11,xend=10,yend=11)) + geom_segment(aes(x=5.5,y=11,xend=5.5,yend=10.5)) + geom_segment(aes(x=10,y=11,xend=10,yend=10.5)) + #bracket 2
geom_text(aes(x=2.5,y=11.5,label="Group 1")) + geom_text(aes(x=7.75,y=11.5,label="Group 2")) + #add labels
coord_cartesian(ylim = c(0, 10), clip="off")+theme(plot.margin = unit(c(4,1,1,0), "lines")) #change plot margins
The following are my r code for the scatterplot.
library(tidyverse)
Pop <-c(24039274, 24854892, 25718048, 26624820, 27568436, 28543940, 29550662, 30590487, 31663896, 32771895)
Popu <- data.frame(Year = 2000:2009, lpop = log2(Pop))
ggplot(Popu, aes(Year, lpop)) +
geom_point(size=3, col = "steelblue") +
ylab("Log2 of Population")
My question is, why the x axis reflects 2000.0, 2002.5 and so on? How do I fix this one?
Thank you in advance.
Try this:
Popu <- data.frame(Year = factor(seq(2000,2009,1)), lpop = log2(Pop))
ggplot(Popu, aes(Year, lpop)) +
geom_point(size=3, col = "steelblue") +
ylab("Log2 of Population")
I'm trying to replace the x-axis labels "A0" and "A1" by one "A" which can be placed in the middle of "A0" and "A1". It would be better if there is a method which works like the following question:
grouping of axis labels ggplot2
By that, I mean to redraw the x-axis only for each group, and leave a blank between groups.
Here is the code I'm working on:
y = 1*round(runif(20)*10,1)
x1 = c("A","B")
x2 = c(0,1)
x = expand.grid(x1,x2)
xy = cbind(x,y)
xy$z = paste(xy$Var1,xy$Var2,sep="")
p <- ggplot(xy, aes(x=factor(z), y=y,fill=factor(Var2)))
p + geom_boxplot() + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C"))
Try this. No need for the variable z, just use position="dodge":
p <- ggplot(xy, aes(x=factor(Var1), y=y,fill=factor(Var2)))
p + geom_boxplot(position="dodge") + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C"))
I want to use ggplo2 to analyse likert scale variables. I would like to get this kind of graphic (below) but I don't know how to add labels on stacked bars and insert different counts and means for each grouping variable and facet variable (for facet_wrap).
I would be grateful for any help!
Data can be obtained from here
My code:
library(ggplot2)
library(scales)
library(RColorBrewer)
ggplot(example,aes(GroupungVar,fill=VarOfInterest)) + geom_bar(position='fill') +
scale_fill_manual(values = (brewer.pal(5, "Greens"))) +
facet_wrap(~FacetVar,ncol=1) + coord_flip() +
scale_y_continuous(labels=percent) + ylab('Percent')
What I get...
..and what I want to achieve (numbers ale not the same as in dataset). I want to have count (N) in labels of each group, percentage labels on bars and mean value on the right side (for each group of course). Percentages and mean values should be for all bars in the plot, I only add them to the first few, just to show what I mean.
I spend night with R and ggplot2 and I get what I wanted :)
library('ggplot2')
library('plyr')
library('RColorBrewer')
library(scales)
label_positions<- function(x) {
n<-length(x)
wynik<-numeric(n)
for (i in 1:n){
if (i==1) {
wynik[i]<-0+x[i]/2
}
else {
wynik[i]<-x[i]-(x[i]-x[i-1])/2
}
}
return(wynik)
}
exam1<-ddply(example,.(GroupingVar,FacetVar,VarOfInterest), 'nrow')
exam1.1<-ddply(example,.(GroupingVar,FacetVar),summarise, sr=mean(as.numeric(VarOfInterest),na.rm=T),
odch=sd(as.numeric(VarOfInterest,na.rm=T)))
exam1<-merge(exam1,exam1.1,by.x=c('GroupingVar','FacetVar'),by.y=c('GroupingVar','FacetVar'))
names(exam1)[4]<-'Count'
exam2<-mutate(exam1,cumul=ave(Count,list(GroupingVar,FacetVar),FUN=cumsum),
N=ave(cumul, list(GroupingVar,FacetVar),FUN=max),
CumSumPercent=cumul/N*100,
Freq=Count/N*100)
exam2<-mutate(exam2,cfrq = ave(CumSumPercent, list(GroupingVar,FacetVar), FUN = label_positions))
exam2$XLabel<-paste(exam2$GroupingVar,' (N=',exam2$N,')',sep='')
exam2$PosMean<-105
p<-ggplot(exam2, aes(x = Etykieta, y = Freq, fill = VarOfInterest)) +
geom_bar(stat = 'identity',colour="black") +
labs (x = "", y = "Percentage", fill=" ") +
scale_fill_brewer(name="Rating", palette="Greens", breaks = rev(levels(exam2$VarOfInterest))) +
geom_text(aes(y = cfrq, label=paste(sprintf("%.01f",Freq), "%", sep='')), size=5) +
geom_text(aes(y=PosMean,label=paste(sprintf("%.02f",sr),' (',sprintf("%.02f",odch),')',sep='')),size=5)+
facet_wrap(~FacetVar,ncol=1) +
coord_flip() + ylab('Procent odpowiedzi') +
guides(fill=guide_legend(title=NULL)) + theme_bw() +
theme(legend.position="bottom",strip.text.x=element_text(size=15,face='bold'),
axis.text.x =element_text(size=12,face='bold'), axis.text.y =element_text(size=12,face='bold'),
axis.title.x=element_text(size=15,face='bold'), axis.title.y=element_text(size=15,face='bold'),
strip.background=element_rect(colour='black'))
plot(p)
And result
For the sample sizes, I would probably just put them in the axis labels, rather than on the graph itself:
library(plyr)
example <- ddply(example,.(FacetVar,GroupungVar),
transform,
GroupingVar = paste(as.character(GroupungVar)," - (n=",length(GroupungVar),")",sep = ""))
ggplot(example,aes(GroupingVar,fill=VarOfInterest)) +
geom_bar(position='fill') +
scale_fill_manual(values = (brewer.pal(5, "Greens"))) +
facet_wrap(~FacetVar,ncol=1) +
coord_flip() +
scale_y_continuous(labels=percent) +
ylab('Percent')