Legend is not shown using ggplot2

Hello,

I'm new using RStudio. I'm trying to show the distribution of temperature values (min,max and mean) in a graphic, my database is called 'datos_diarios' and I already imported the library, but it doesn't seem to work with this code...
It also shows the values on the X axis way too close and the can't be seen properly.

Can someone help me?
Thanks in advance!

ggplot(datos_diarios,aes(x=dias))+
  geom_line(aes(y=Max_Abs,group=1),color='grey',size=0.35)+
  geom_line(aes(y=Min_Abs,group=1),color='grey',size=0.35)+
  geom_line(aes(y=Valor,group=1),color='blue',size=0.5)+
  geom_point(aes(y =Valor),color='red',size = 0.6)+
  labs(x="Fecha",y="Temperatura [ºC]",title='Temperatura Diaria del Aire')+
  scale_color_manual(name="Leyenda",values=c("Valores extremos"="grey","Temperatura diaria"="blue"))+
  theme(legend.position='right',plot.title=element_text(hjust=0.5))

Thanks for providing code. Could you kindly take further steps to make it easier for other forum users to help you? Share some representative data that will enable your code to run and show the problematic behaviour.

How do I share data for a reprex?

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

Here I show the structure of the database and the graphic that I get, hope it helps...

I added a capture of the database and the plot, hope it helps

Screenshots are better than nothing.. but not great.

I would guess from that, that your date variable is simply not a date type, but probably a mere character string containing information that you percieve as dates, but that R doesnt consider, R thinks they are literal labels.

try using lubridate packages functions to make a date from it and try again.

The database I'm trying to plot comes from hourly data grouped by days with this function, 'Fecha' means 'date'

data<-read.table(file="C:/_R/M5074-Puntas_TempAire.txt",sep="\t",header=T)
data<-na.omit(data) 

data$Fecha<-as.POSIXct(data$Fecha,format="%Y-%m-%d %H:%M",tz="America/Guayaquil")

there is an implied relationship between Fecha that you describe now and dias that you originally presented, but its unclear to me what it is. I struggle to explain what I havent been shown.

This is how I grouped the data

data$dias<-droplevels(cut(data$Fecha,breaks = 'day'))
datos_diarios<-aggregate(cbind(Valor,Max_Abs,Min_Abs)~dias,data=data,FUN=mean)

cutting them makes them into factors, if you want them as dates you shouldnt use cut etc.

# going from a datetime to a date i.e. only caring about days

(not_great <- cut(Sys.time(),breaks="day") )
str(not_great)

(better<- lubridate::as_date(Sys.time()))
str(better)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.