Area sotto la curva ggplotly R non resa come previsto

Aug 16 2020

Ho creato un grafico con il seguente set di dati e ggplot2

Ho scaricato il set di dati in CSV dal link sottostante

https://www.kaggle.com/dataset/e392d3cfbb5749653d5c82f4bec1daa03628fb06d374fad84eac319f1b3f982422

Il codice che ho usato è il seguente

 library(readr)
 library(ggplot2)
 library(plotly)

Successivamente creo il dataframe dal file CSV

DF <- read_csv("C:/Users/mysystem/Desktop/Random3.csv")####Please set your working directory here

Ora creo una trama usando 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') 

Il grafico sopra mostra correttamente l'area sotto le curve. Tuttavia, quando eseguo ggplotly, l'area sotto le curve viene invertita

ggplotly(p2)

C'è un modo in cui questo può essere evitato. L'area ombreggiata sembra spostarsi oltre l'area sotto le curve in alcuni casi e in questo caso sembra spostarsi nell'inverso della curva. chiedo a qualcuno di dare un'occhiata

Risposte

1 AllanCameron Aug 16 2020 at 19:25

Penso che tu voglia solo geom_areainvece di geom_ribbon. Puoi anche impostare il primo valore su 0 invece di saltarlo per evitare il riempimento inverso che ha il poligono risultante.

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)