stacked area plots ggplot - r

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

Related

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))

R - Bar Plot with transparency based on values?

I have a dataset myData which contains x and y values for various Samples. I can create a line plot for a dataset which contains a few Samples with the following pseudocode, and it is a good way to represent this data:
myData <- data.frame(x = 290:450, X52241 = c(..., ..., ...), X75123 = c(..., ..., ...))
myData <- myData %>% gather(Sample, y, -x)
ggplot(myData, aes(x, y)) + geom_line(aes(color=Sample))
Which generates:
This turns into a Spaghetti Plot when I have a lot more Samples added, which makes the information hard to understand, so I want to represent the "hills" of each sample in another way. Preferably, I would like to represent the data as a series of stacked bars, one for each myData$Sample, with transparency inversely related to what is in myData$y. I've tried to represent that data in photoshop (badly) here:
Is there a way to do this? Creating faceted plots using facet_wrap() or facet_grid() doesn't give me what I want (far too many Samples). I would also be open to stacked ridgeline plots using ggridges, but I am not understanding how I would be able to convert absolute values to a stat(density) value needed to plot those.
Any suggestions?
Thanks to u/Joris for the helpful suggestion! Since, I did not find this question elsewhere, I'll go ahead and post the pretty simple solution to my question here for others to find.
Basically, I needed to apply the alpha aesthetic via aes(alpha=y, ...). In theory, I could apply this over any geom. I tried geom_col(), which worked, but the best solution was to use geom_segment(), since all my "bars" were going to be the same length. Also note that I had to "slice" up the segments in order to avoid the problem of overplotting similar to those found here, here, and here.
ggplot(myData, aes(x, Sample)) +
geom_segment(aes(x=x, xend=x-1, y=Sample, yend=Sample, alpha=y), color='blue3', size=14)
That gives us the nice gradient:
Since the max y values are not the same for both lines, if I wanted to "match" the intensity I normalized the data (myDataNorm) and could make the same plot. In my particular case, I kind of preferred bars that did not have a gradient, but which showed a hard edge for the maximum values of y. Here was one solution:
ggplot(myDataNorm, aes(x, Sample)) +
geom_segment(aes(x=x, xend=x-1, y=Sample, y=end=Sample, alpha=ifelse(y>0.9,1,0)) +
theme(legend.position='none')
Better, but I did not like the faint-colored areas that were left. The final code is what gave me something that perfectly captured what I was looking for. I simply moved the ifelse() statement to apply to the x aesthetic, so the parts of the segment drawn were only those with high enough y values. Note my data "starts" at x=290 here. Probably more elegant ways to combine those x and xend terms, but whatever:
ggplot(myDataNorm, aes(x, Sample)) +
geom_segment(aes(
x=ifelse(y>0.9,x,290), xend=ifelse(y>0.9,x-1,290),
y=Sample, yend=Sample), color='blue3', size=14) +
xlim(290,400) # needed to show entire scale

Why is my R bar chart (geom_bar) not appropriately filling color based on the provided variable?

This is a bit of a newbie question. I am using the package "nycflights13" in R, and "tidyverse".
library(nycflights13)
library(tidyverse)
I am trying to get a bar chart that shows the total number of flights by airline/carrier, and have it color each bar by the number of flights that occurred each month.
I can get a simple bar chart to show with the following:
ggplot(flights) +
geom_bar(mapping=aes(x=carrier))
When I try to color it with the month, it doesn't change anything.
ggplot(flights) +
geom_bar(mapping=aes(x=carrier, fill=month))
The graph generated by the code above looks exactly the same.
It seems to work when I do the opposite... if I create a chart with "month" on the x-axis and color by carrier, it works just like I would expect.
ggplot(flights) +
geom_bar(mapping=aes(x=month,fill=carrier))
I assume it has something to do with discrete vs continuous variables?
Yes, this has to do with discrete vs continuous variables. as.factor() will convert month to discrete factors.
ggplot(flights) +
geom_bar(mapping=aes(x=carrier, fill=as.factor(month)))
For fun, there is a way to override geom_bar's built in stat_count default. This requires adding a dummy variable to flights, to use as a y, and sorting the data by month (or you get weird artifacts). Look at the help document about ?geom_bar().
flights$n<-1
flights%>%
arrange(month)%>%
ggplot(aes(carrier, n, fill = month)) +
geom_bar(stat = "identity") +
scale_fill_continuous(low="blue", high="red")

Making line plot with discrete x-axis in ggplot2

I am building a ggplot2 figure with a facet grid. On my Y-axis are percentages, and my X-axis is the concentration (in numbers). Each facet has 3 groups (0, 24 and 48 hours)
ggplot(data=MasterTable, aes(x=Concentration, y=Percentage, group=Time)) +
geom_point() +
geom_line() +
facet_grid(Chemicals ~ Treatments)
This generates a continuous x-axis. Since the values are not evenly spread out, I would prefer a discrete axis to better visualize my data. I followed the following tutorial with no luck. The first figure is exactly what I am trying to do.
I also tried formatting the axis:
scale_x_discrete(labels("0", "0.1", "2", "50"))
and formatting the line:
geom_line(aes(Time))
and following this tutorial.
I think this problem is that the values on the x-axis are integers rather than strings. This makes the default axis continuous. How can I change this?? I am sure the solution is simple, I just can't figure it out.
Thanks in advance!
On this page they make the following modification df2$dose<-as.factor(df2$dose). You can try to modify your x-axis as df2$Concentration<-as.factor(df2$Concentration)
or like this:
ggplot(data=MasterTable, aes(x=factor(Concentration), y=Percentage, group=Time)) +
geom_point() +
geom_line() +
facet_grid(Chemicals ~ Treatments)

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)

Resources