Geom_area() with discrete x-axis

304 Views Asked by At

I have the following dataset:

enter image description here

and I want a plot with PorcentajeElemento in the x-axis, PorcentajeNR in the y-axis, the colors according to UnidadAnatómica, but I would like to see it with different areas. So I have the following code:

library(ggplot2)
library(openxlsx)

Dataset<- read.xlsx("porcentaje elemento.xlsx",rowNames=FALSE, colNames=TRUE, sheet="Los 
Cinchos")

ggplot(Dataset, mapping=aes(x=PorcentajeElemento, 
y=PorcentajeNR,fill=UnidadAnatómica),stat="identity")+
  facet_grid(rows=vars(Dataset$UnidadAnatómica))+
  geom_area(color=1,lwd=0.5,linetype=1)+
  theme_bw()

and I get the following plot:

enter image description here

As you can see, there is a mistake in the graph, and I guess that the reason is that x is "discrete". If I create another continuous variable (e.g. Dataset$B) for the x-axis, it works:

 ggplot(Dataset, mapping=aes(x=B, y=PorcentajeNR,fill=UnidadAnatómica),stat="identity")+
  facet_grid(rows=vars(Dataset$UnidadAnatómica))+
  geom_area(color=1,lwd=0.5,linetype=1)+
  theme_bw()

I get the following plot:

enter image description here

I want this last plot, but with Dataset$PorcentajeElemento in the x-axis. Is this possible? Thank you

0

There are 0 best solutions below