Plot dual y-axis

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
Apartment_no <- c("1-SV","1-SV","1-SV","1-SH","1-SH","1-SH","1-1V","1-1V","1-1V","1-1H","1-1H","1-1H","3-SV","3-SV","3-SV","3-1V","3-1V","3-1V","3-1H","3-1H","3-1H")
month <- c("September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November")
Days <- c("2","19","28","2","19","28","2","19","28","2","19","28","25","31","28","12","13","24","8","26","19")
Heat_data <- data.frame(Apartment_no,month,Days)

I am given the following data. I just wanted to plot it in the most effective way so that I can know for which apartment, & for for how many days, I have the data corresponding to which month. I just want my readings to appear as dots on the graph. So far, I tried the following 3 codes but they really haven't worked in my case.

>Heat_data%>%ggplot()+geom_point(aes(x=month,y=Apartment_no),col="red")+geom_point(x=month,y=Days,col="blue")+scale_y_continuous(name = expression("Apartment_no"),sec.axis = sec_axis(name = "Days"))

> Heat_data%>%ggplot()+geom_point(mapping = aes(x=month,y=Apartment_no,size=3,shape=21,fill="blue"))+scale_y_continuous(name = expression("Apartment_no"),sec.axis = sec_axis(name = Days)) 
>p <- ggplot(Heat_data,aes(month,Apartment_no))+geom_point()
p+scale_y_continuous(sec.axis = sec_axis(Days))

Furthermore I also tried something with facet_wrap, but it gives me a weird graph which is not understandable.

>Heat_data%>%ggplot(aes(month,value,fill=Apartment_no))+geom_col()+coord_flip()+facet_wrap(~Apartment_no)

Also I want the dots to appear highlighted with red color where ever my days reading falls below 8.

The main problem is probably that you have entered the values in the Days vector as text by putting double quotes around the values. Notice that column is a factor in the output of the str() function. I converted them to numbers and then the graph looks reasonable as a facet plot.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
Apartment_no <- c("1-SV","1-SV","1-SV","1-SH","1-SH","1-SH","1-1V","1-1V","1-1V","1-1H","1-1H","1-1H","3-SV","3-SV","3-SV","3-1V","3-1V","3-1V","3-1H","3-1H","3-1H")
month <- c("September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November","September","October","November")
Days <- c("2","19","28","2","19","28","2","19","28","2","19","28","25","31","28","12","13","24","8","26","19")
Heat_data <- data.frame(Apartment_no,month,Days)
str(Heat_data)
#> 'data.frame':    21 obs. of  3 variables:
#>  $ Apartment_no: Factor w/ 7 levels "1-1H","1-1V",..: 4 4 4 3 3 3 2 2 2 1 ...
#>  $ month       : Factor w/ 3 levels "November","October",..: 3 2 1 3 2 1 3 2 1 3 ...
#>  $ Days        : Factor w/ 10 levels "12","13","19",..: 4 3 8 4 3 8 4 3 8 4 ...
Heat_data$Days <- as.numeric(Heat_data$Days)
ggplot(Heat_data, aes(month, Days)) + geom_point() + facet_wrap(~Apartment_no)

Created on 2019-11-18 by the reprex package (v0.3.0.9000)

1 Like

@FJCC, Thanks for your help. But since, I have a lot of apartments to show, & I just figured out that adding another y-axis is not going to help. I also tried, one more code as, & I am getting something which can be seen in the image. I am trying to make it interactive somehow coz that way it would help.

ggplot(Heat_clean,aes(x=month,y=Days,group=Apartment_no,color=Apartment_no,size=Days))+geom_point(stat ='identity')

But using this one, I am getting something like this.

I don't know of a good way to plot so many categories. I would still be inclined to try a facet plot using lines instead of points, having no text on the tic marks of either axis and opening the largest plotting window possible. If you are on Windows, you can use the command

windows(width = 20, height = 16)

to open a plotting window. Adjust the height and width to the largest size your system will support. The equivalent command on Linux is X11().

1 Like

I think you should experiment with different approaches for aesthetic mapping, like this one for example.

library(ggplot2)

Heat_data <- data.frame(
    Days = c(4, 3, 8, 4, 3, 8, 4, 3, 8, 4, 3, 8, 6, 9, 8, 1, 2, 5, 10,
             7, 3),
    Apartment_no = as.factor(c("1-SV", "1-SV", "1-SV", "1-SH", "1-SH", "1-SH",
                               "1-1V", "1-1V", "1-1V", "1-1H", "1-1H", "1-1H",
                               "3-SV", "3-SV", "3-SV", "3-1V", "3-1V", "3-1V",
                               "3-1H", "3-1H", "3-1H")),
    month = factor(c("September", "October", "November", "September",
                        "October", "November", "September", "October",
                        "November", "September", "October", "November",
                        "September", "October", "November", "September",
                        "October", "November", "September", "October",
                        "November"), levels = c("September", "October", "November")) 
)

ggplot(Heat_data, aes(x = month, y = Apartment_no, fill = Days)) +
    geom_raster()

1 Like

Thanks a lot for your keen efforts @FJCC, @andresrcs but I have moved with an interactive chart using the following code:

q <-qplot(x=month,y=Days,data = Heat_clean,color = Apartment_no)
plot(q)
ggplotly(q)

I also figured out a way to share this to anyone.

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