Home>

R is a super beginner.
I have a DataFrame as shown below. I want to create a boxplot + dotplot overlay graph with pre, 0min, 1min, 5min, ...., 30min as the X axis, but it doesn't work. You can use straight line graphs of the mean and standard deviation directly from these data, but how do you code them?
If there is raw data of"data_raw" ;, if you name the column and then try to draw a graph, plot will not appear. I don't know how to define y. I think it's a basic question, but I hope someone can help.
Thank you.

colnames (data_raw)<-c ("pre","0min","1min","5min","10min","15min","20min","25min" ;,"30min")
library (ggplot2)
ggplot (data = data_raw, aes (x = colnames, y = count)) + geom_boxplot ()

pre 0min 1min 5min 10min 15min 20min 25min 30min
1 303 312 324 304 324 326 339 345 346
2 278 316 315 301 313 313 305 310 310
3 194 217 216 205 213 212 215 221 222
4 150 170 170 162 170 173 171 173 173
5 84 94 94 91 94 97 98 100 102
6 224 241 240 228 241 248 251 252 238

r
  • Answer # 1

    In an early stage, when you try to apply colnames count that is not defined anywhere to x, y suddenly, it should not work.

    For example, here is an example.

    Look closely at the data frames used in the example and how to use ggplot (). And compare with the program you wrote.

    Addendum: Data frame transformation

    My guidance seems bad. What should be considered was the procedure of transforming dataframe rather than using ggplot.

    In order to use with ggplot, the data frame sequence and x and y on the graph must be in a 1: 1 correspondence.
    There is a convenient function for that.

    library (dplyr)
    data_raw<-read.table (text = "
    303 312 324 304 324 326 339 345 346
    278 316 315 301 313 313 305 310 310
    194 217 216 205 213 212 215 221 222
    150 170 170 162 170 173 171 173 173
    84 94 94 91 94 97 98 100 102
    224 241 240 228 241 248 251 252 238
    ")
    colnames (data_raw)<-c ("pre", "0min", "1min", "5min", "10min", "15min", "20min", "25min", "30min")
    data_mod<-data_raw%>% tidyr :: gather (key = time, value = count)


    Compare the original data (data_raw) with the transformed data (data_mod).
    You can see that the data divided into multiple columns is grouped into one column, and the name of the column to which each data originally belonged is described in a new column ("time").

    Now we have a line that can be used as x and y of ggplot. Specify this column name in the aes of ggplot. Maybe you can draw the graph you are looking for.

    g<-ggplot (data_mod, aes (x = time, y = count)) + geom_boxplot ()
    plot (g)