Barchart with ggplot 2 y axis labels - r

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)

Related

R: Months on x axis and a line for every year

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)

Geom_area plot doesn't fill the area between the lines

I want to make an area plot with ggplot(mpg, aes(x=year,y=hwy, fill=manufacturer)) + geom_area(), but I get this:
I'm realy new in R world, can anyone explain why it does not fill the area between the lines? Thanks!
First of all, there's nothing wrong with your code. It's working as intended and you are correct in the syntax required to do what you are looking to do.
Why don't you get the area geom to plot correctly, then? Simple answer is that you don't have enough points to draw a proper line between your x values for all of the aesthetics (manufacturers). Try the geom_point plot and you'll see what I mean:
ggplot(mpg, aes(x=year,y=hwy)) + geom_point(aes(color=manufacturer))
You need a different dataset. Here's a dummy one that is simply two lines with different slopes. It works as expected because each of the aesthetics has y values which span the x labels:
# dummy dataset
df <- data.frame(
x=rep(1:10,2),
y=c(seq(1,10,length.out=10), seq(1,5,length.out=10)),
z=c(rep('A',10), rep('B', 10))
)
# plot
ggplot(df, aes(x,y)) + geom_area(aes(fill=z))

How to change geom_text to percentage in ggplot2

I'm trying to convert the decimal points from the colomn titled cover_type_ratio below into a percentage in ggplot2. Here is the df
DF
And here is my code for my bar graph:
plot<-ggplot(coverType_count, aes(x=Cover_Type, y=count)) +
geom_bar(stat="identity") +
scale_y_continuous() +
geom_text(data=coverType_count,aes(label=count,y=count+100),size=4) +
geom_text(data=coverType_count,
aes(label=cover_type_ratio,y=count+200),size=4)+
theme(axis.text.x=element_text(angle=30,hjust=1,size=8))+
ggtitle('Cover Type Distribution')
When plotted, it looks like this:
Screenshot of Plot
I need to change the jumbled numbers to percentages from the aforementioned cover_type_ratio column in df, and have tried using the scales library, but to no avail. Does anyone have any suggestions?
Thank you!

stacked area plots ggplot

I was hoping someone might be able to help. I am still getting to grips with R and I am quite new to ggplot2.
My problem:
I am trying to make a stacked area plot. I have formatted my data frame so that it is in long format. My columns are Date, Category (filter.size) and value (chl.average).
e.g:
data frame example
The issue I am having is that when I try and plot this, where Chlstacked is my data.frame):
stkchl <- ggplot(Chlstacked, aes(x=Date, y=chl.average,
fill=filter.size)) + geom_area()
stkchl
the axis and background layer plots but not the actual stack, although it recognises the categories in a legend with colours.
I have tried and alternate method:
stkchl <- ggplot(Chlstacked, aes(x=Date, y=chl.average))
stkchl
stkchl + geom_area(aes(colour = chl.average, fill= chl.average),
position = 'stack')
Which gives: Error in f(...) : Aesthetics can not vary with a ribbon
My thought is that perhaps as the Dates, which I would want on the x-axis (as it is time series), are repeated for each category (>20, <20>5, <5>GFF) they are not unique so maybe doing something - altough I am stumped as to what - to cause error.Or perhaps something simple that I am doing wrong within my coding?
Any help would be appreciated - thanks

ggplot: How to increase space between axis labels for categorical data?

I love ggplot, but find it hard to customize some elements such as X axis labels and grid lines. The title of the question says it all, but here's a reproducible example to go with it:
Reproducible example
library(ggplot2)
library(dplyr)
# Make a dataset
set.seed(123)
x1 <- c('2015_46','2015_47','2015_48','2015_49'
,'2015_50','2015_51','2015_52','2016_01',
'2016_02','2016_03')
y1 <- runif(10,0.0,1.0)
y2 <- runif(10,0.5,2.0)
# Make the dataset ggplot friendly
df_wide <- data.table(x1, y1, y2)
df_long <- melt(df_wide, id = 'x1')
# Plot it
p <- ggplot(df_long, aes(x=x1,
y=value,
group=variable,
colour=variable )) + geom_line(size=1)
plot(p)
# Now, plot the same thing with the same lines and numbers,
# but with increased space between x-axis labels
# and / or space between x-axis grid lines.
Plot1
The plot looks like this, and doesn't look too bad in it's current form:
Plot2
The problem occurs when the dataset gets bigger, and the labels on the x-axis start overlapping each other like this:
What I've tried so far:
I've made several attempts using scale_x_discrete as suggested here, but I've had no luck so far. What really bugs me is that I saw some tutorial about these things a while back, but despite two days of intense googling I just can't find it. I'm going to update this section when I try new things.
I'm looking forward to your suggestions!
As mentioned above, assuming that x1 represents a year_day, ggplot provides sensible defaults for date scales.
First make x1 into a valid date format, then plot as you already did:
df_long$x1 <- strptime(as.character(df_long$x1), format="%Y_%j")
ggplot(df_long, aes(x=x1, y=value, group=variable, colour=variable)) +
geom_line(size=1)
The plot looks a little odd because of the disconnected time series, but scales_x_date() provides an easy way to customize the axis:
http://docs.ggplot2.org/current/scale_date.html

Resources