R: Months on x axis and a line for every year - r
I'm trying to plot a dataset like the one in the picture.
Example dataset
I want a graph for every description with months on the x axis and a line for every year.
I'm using the following code
UniqueObjects <- unique(df$description)
for (i in UniqueObjects){
ggplot(df, aes(month, value, color = year)) +
geom_line()
}
My graph doesn't show the result i want.
Exapmple Graph
Can someone please help me?
How about this?
df<-data.frame("description"=c(rep("a",7),rep("b",8),rep("c",8)),"year"=c(2014,2014,2014,2015,2015,2017,2017,2014,2014,2015,2015,2015,2015,2017,2017,2017,2014,2015,2015,2015,2017,2017,2017),"month"=c(2,3,4,1,3,8,9,5,6,2,3,4,1,2,4,1,12,10,11,12,7,8,9),"value"=c(89,78,13,25,45,11,12,3,6,8,34,56,86,99,88,44,23,11,5,7,78,11,12))
for(i in unique(df$description)){
dfsubset<-subset(df,df$description==i)
assign(paste0("plot_",i),ggplot(dfsubset,aes(x=month,y=value,color=as.factor(year)))+geom_line()+theme_classic()+scale_x_continuous(breaks=1:12,limits=c(0,12)))
}
grid.arrange(plot_a,plot_b,plot_c)
Related
How can I get grouped boxplots for each value on the x axis?
I am using a code suggested by someone in another question about grouped boxplots, but mine looks like this instead. I should have 3-4 boxplots (Week in code below) per treatment but they aren't showing up. How can I fix this? w <- Exp1 %>% ggplot(aes(Treatment, `Vertical Travel Distance (mm)`, fill=Week)) + scale_x_discrete(limits=unique(Exp1$Treatment)) + geom_boxplot()
geom_line not outputting connected points
I have the attached dataframe. I am wanting to create a line graph using ggplot in order to plot Total and Year, with seperate lines for each offence category. I have used the following code, but I feel it is very incorrect as the output does not have any connected lines, it looks more like a vertical line graph. Any help is much appreciated :) Dataframe The code I have tried is: ggplot(data = annual, aes(x = (as.numeric(Year)), y = Total, group = Offence Category)) + geom_line()
labels right next points in gg plot
Ive tried to google my way to the answere to the question, but none seems to give the answer to what im trying to do. My goal is to add legends right besides the observations within the plot to show the name of the observation. Name of observations are located in the first column of my data frame. ggplot(data = coef.vec)+aes(x = coef.x, y = variable.mean) + geom_point()
You can use labels with geom_text() in next style. I have used simulated data: library(tidyverse) #Code data <- data.frame(group=paste0('Obs',1:10), coef.x=rnorm(10,0,1), variable.mean=runif(10,0.015,0.05),stringsAsFactors = F) #Plot ggplot(data,aes(x=coef.x,y=variable.mean))+ geom_point()+ geom_text(aes(label=group),hjust=-0.15) Output:
R: ggplot. Axis labels in a boxplot for loop
I am using a for loop to create multiple box plots for a large dataset I have (320269 observables of 170 variables). For this I am using the following code to generate the boxplots: nm <- names(data) for (i in 1:(ncol(data)-1)){ print(ggplot(data,aes(as.factor(data$Month),data[c(i)],color=as.factor(data$Month),aes_string("Month",nm[i]))) + geom_boxplot(outlier.colour="black",outlier.shape=16,outlier.size=1,notch=FALSE))} The graphs are printed in pdf and the boxplot itself comes out correctly, but something goes wrong with the axis labels. No matter what I try, I get the x-axis label: as.factor(data$Month), and on the y-axis: data[c(i)], instead of "Month" on the x-axis and the actual column-names from the dataset on the y-axis. What am I missing? Your help is much appreciated.
You can specify x and y axis labels by + xlab() and + ylab() for (i in 1:(ncol(data)-1)){ print(ggplot(data,aes(as.factor(data$Month),data[c(i)],color=as.factor(data$Month))) + geom_boxplot(outlier.colour="black",outlier.shape=16,outlier.size=1,notch=FALSE) + xlab("Month") + ylab(colnames(data)[i]) ) }
Barchart with ggplot 2 y axis labels
I have a little problem with a ggplot barchart. I wanted to make a barchart with ggplot2 in order to compare my Svolumes for my 4 stocks on a period of few months. I have two problems: The first one is that my y axis is wrong. My graph/data seems correct but the y axis don't "follow" as I thought it will contain another scale... I would to have to "total" number of my dataset svolumes, I think here it is writing my svolumes values. I don't know how to explain but I would like the scale corresponding to all of my data on the graph like 10,20,etc until my highest sum of svolumes. There is my code: Date=c(rep(data$date)) Subject=c(rep(data$subject)) Svolume=c(data$svolume) Data=data.frame(Date,Subject,Svolume) Data=ddply(Data, .(Date),transform,pos=cumsum(as.numeric(Svolume))-(0.5*(as.numeric(Svolume)))) ggplot(Data, aes(x=Date, y=Svolume))+ geom_bar(aes(fill=Subject),stat="identity")+ geom_text(aes(label=Svolume,y=pos),size=3) and there is my plot: I helped with the question here Finally, How could I make the same plot for each months please? I don't know how to get the values per month in order to have a more readable barchart as we can't read anything here... If you have other ideas for me I would be very glad to take any ideas and advices! Maybe the same with a line chart would be more readable...? Or maybe the same barchart for each stocks ? (I don't know how to get the values per stock either...) I just found how to do it with lines.... but once again my y axis is wrong, and it's not very readable.... Thanks for your help !! :)
Try adding the following line right before your ggplot function. It looks like your y-axis is in character. [edit] Incorporate #user20650's comments, add as.character() first then convert to numeric. Data$Svolume <- as.numeric(as.character(Data$Svolume)) To produce the same plot for each month, you can add the month variable first: Data$Month <- month(as.Date(Date)). Then add facet to your ggplot object. ggplot(Data, aes(x=Date, y=Svolume) + ... + facet_wrap(~ Month) For example, your bar chart code will be: Data$Svolume <- as.numeric(as.character(Data$Svolume)) Data$Month <- month(as.Date(Date)) ggplot(Data, aes(x=Date, y=Svolume)) + geom_bar(aes(fill=Subject),stat="identity") + geom_text(aes(label=Svolume,y=pos),size=3) + facet_wrap(~ Month) and your Line chart code will be: Data$Svolume <- as.numeric(as.character(Data$Svolume)) Data$Month <- month(as.Date(Date)) ggplot(Data, aes(x=Date, y=Svolume, colour=Subject)) + geom_line() + facet_wrap(~ Month)