I have created a plot with the following dataset and ggplot2
I downloaded the dataset in CSV from the below link
https://www.kaggle.com/dataset/e392d3cfbb5749653d5c82f4bec1daa03628fb06d374fad84eac319f1b3f982422
The code I have used is as follows
library(readr)
library(ggplot2)
library(plotly)
Next I create the dataframe from the CSV file
DF <- read_csv("C:/Users/mysystem/Desktop/Random3.csv")####Please set your working directory here
Now I create a plot using ggplot
p2<-ggplot(DF, aes(x=Col1, y=Col2, group=ID)) +
geom_line(size=.5) + geom_ribbon(data=subset(DF, Col1>1 ),aes(x=Col1,ymax=Col2,
fill=ID),ymin=0,alpha=0.3 ) +
scale_fill_manual(name='Legend', values=c("green4", "red"), labels=c("A", "B" ))+labs(x="Col1",
y="Col2")+ xlim(0, 10000)+ theme_bw()# +theme(legend.position='none')
The above plot shows the area under the curves properly. When i run ggplotly however, the area under curves get inverted
ggplotly(p2)
Is there a way this can be avoided. The Shaded area appears to move beyond the area under the curves in some cases and in this case seems to move to the inverse of the curve. i request someone to take a look.
I think you're just wanting geom_area
instead of geom_ribbon
. You can also set the first value to 0 instead of skipping it to prevent the reverse filling that the resultant polygon has.
library(ggplot2)
library(plotly)
DF <- read.csv("Random3.csv")
DF$Col2[DF$Col1 == 0] <- 0
p2 <- ggplot(DF, aes(x = Col1, y = Col2, group = ID)) +
geom_line(size = 0.5) +
geom_area(aes(x = Col1, fill = ID), alpha = 0.3,
position = "identity") +
scale_fill_manual(name = 'Legend',
values = c("green4", "red"),
labels = c("A", "B")) +
labs(x = "Col1", y = "Col2") +
xlim(0, 10000) +
theme_bw()
p2
ggplotly(p2)