简体   繁体   中英

R program visualization + How to plot between categorical and numerical data

I want to develop a visualization graph with date in x axis and A,B,C values in Y axis. A value categorical data and B,c are numerical data. In the x axis, date should be represented in month and day like (01/07, 02/07). The problem here is A value is categorical data and B value is numerical data. The visualization charts should not be of bar type.It is like scatter plots.

I didn't know how to do this one. Your help would be highly appreciable. Thanks.

     dput(df)

     structure(list(Date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
     0L, 0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
     0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
     0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = 1:31, mon = c(6L, 6L, 6L, 
     6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
     6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), year = c(116L, 
     116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 
     116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L, 
     116L, 116L, 116L, 116L, 116L, 116L, 116L, 116L), wday = c(5L, 
     6L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 0L, 
     1L, 2L, 3L, 4L, 5L, 6L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 0L), yday =                         182:212, 
isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), zone = c("JST", "JST", "JST", "JST", "JST", 
"JST", "JST", "JST", "JST", "JST", "JST", "JST", "JST", "JST", 
"JST", "JST", "JST", "JST", "JST", "JST", "JST", "JST", "JST", 
"JST", "JST", "JST", "JST", "JST", "JST", "JST", "JST"), 
gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_)), .Names = c("sec", "min", "hour", 
"mday", "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"
), class = c("POSIXlt", "POSIXt"), tzone = "Asia/Tokyo"), A =     
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,    
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("N",    
"YES"), class = "factor"), B = c(0L, 0L, 0L, 0L, 1L, 3L, 5L, 
10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), C = c(0L, 0L, 0L, 0L, 1L, 
3L, 5L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 5L, 10L, 9L, 
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 10L, 9L, 8L)), .Names = c("Date", 
"A", "B", "C"), row.names = c(NA, -31L), class = "data.frame")


        sample data:

        Date   A  B  C
        1  2016-07-01   N  0  0
        2  2016-07-02   N  0  0
        3  2016-07-03   N  0  0
        4  2016-07-04   N  0  0
        5  2016-07-05   N  1  1
        6  2016-07-06   N  3  3
        7  2016-07-07   N  5  5
        8  2016-07-08   N 10 10
        9  2016-07-09   N  9  9
        10 2016-07-10   N  8  8
        11 2016-07-11   N  7  7
        12 2016-07-12   N  6  6
        13 2016-07-13   N  5  5
        14 2016-07-14   N  4  4
        15 2016-07-15   N  3  3
        16 2016-07-16   N  2  2
        17 2016-07-17   N  1  1
        18 2016-07-18   N  0  5
        19 2016-07-19   N  0 10
        20 2016-07-20   N  0  9
        21 2016-07-21   N  0  8
        22 2016-07-22   N  0  7
        23 2016-07-23   N  0  6
        24 2016-07-24 YES  0  5
        25 2016-07-25   N  0  4
        26 2016-07-26   N  0  3
        27 2016-07-27   N  0  2
        28 2016-07-28   N  0  1
        29 2016-07-29   N  0 10
        30 2016-07-30   N  0  9
        31 2016-07-31   N  0  8

I tried this code. It works good for one value like B.. when I try to plot C value along with B, the graph seems different .

 library(scales)
 library(ggplot2)
 ggplot(df, aes(x = Date, y = B)) +
 geom_line(aes(y=B,group=1),colour="#000099") +
 geom_point(size=2, colour="#CC0000") +   
 scale_y_continuous(breaks = seq(0, 50, by = 1)) + 
 scale_x_datetime(date_breaks = "2 day")

Here's one possible way to do it. I am not sure if this is ideal but hopefully helps in your case:

Using Markers

#library(devtools)
#install_github("ropensci/plotly")

library(plotly)
library(zoo)

df$Date <- as.yearmon(df$Date)
df$Date <- as.Date(df$Date)

plot_ly(df, x = ~Date) %>% 
    add_markers(y = ~B, marker = list(size = 15, symbol = "cross"), 
                name = "B") %>% 

    add_markers(y = ~C, marker = list(size = 10, symbol = "circle"),
                name = "C") %>% 

    add_markers(y = ~A, marker = list(size = 20, symbol = "diamond-open"),
                name = "A", yaxis = "y2") %>% 

    layout(xaxis = list(domain = c(0, 0.9)),
           yaxis2 = list(side = "right", anchor = "xaxis", overlaying = "y", title = "A"),
           yaxis = list(title = "B/C"))

在此处输入图片说明

Using lines

#library(devtools)
#install.github("ropensci/plotly")

library(plotly)

df$x <- 1:nrow(df)
df$Date <- as.character(as.Date(df$Date))

plot_ly(df, x = ~x) %>% 

    add_lines(y = ~B, line = list(width = 5), opacity = "0.5",
              name = "B") %>% 

    add_lines(y = ~C, line = list(width = 5, dash = "5px"),
              name = "C") %>% 

    add_markers(y = ~A, marker = list(size = 15, symbol = "cross"),
                name = "A", yaxis = "y2") %>% 

    layout(xaxis = list(domain = c(0, 0.9), title = "",
                        tickmode = "array",
                        tickvals = ~x,
                        ticktext = ~Date,
                        tickfont = list(size = 10)),
           yaxis2 = list(side = "right", anchor = "xaxis", overlaying = "y", title = "A"),
           yaxis = list(title = "B/C"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM