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

时间:2017-03-20 04:58:14

标签: r ggplot2 numerical

我想开发一个可视化图形,其中x轴为日期,Y轴为A,B,C值。值分类数据和B,c是数值数据。在x轴上,日期应以月份和日期表示,如(01 / 07,02 / 07)。这里的问题是A值是分类数据,B值是数值数据。可视化图表不应该是条形图。它就像散点图。

我不知道怎么做这个。你的帮助会非常明显。感谢。

     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

我试过这段代码。它适用于像B这样的一个值。当我尝试将C值与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")

1 个答案:

答案 0 :(得分:0)

这是一种可行的方法。我不确定这是否理想,但希望对你的情况有所帮助:

使用标记

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

enter image description here

使用行

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

enter image description here