简体   繁体   English

R程序可视化+如何在分类数据和数值数据之间进行绘图

[英]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. 我想开发一个可视化图形,其中x轴为日期,Y轴为A,B,C值。 A value categorical data and B,c are numerical data. 值分类数据和B,c是数字数据。 In the x axis, date should be represented in month and day like (01/07, 02/07). 在x轴上,日期应以月和日表示,例如(01/07,02/07)。 The problem here is A value is categorical data and B value is numerical data. 这里的问题是A值是分类数据,B值是数字数据。 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 . 当我尝试将C值与B一起绘制时,它对于像B.这样的一个值效果很好。

 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"))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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