Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

library(stringr)

DF <- data.frame(group = c("SL", "AUS", "Italy"),

day1    =   c(1 ,   4   ,   58),

day2    =   c(2 ,   4   ,   78),

day3    =   c(2 ,   6   ,   72),

day4    =   c(5 ,   14  ,   94),

day5    =   c(7 ,   7   ,   147),

day6    =   c(10,   3   ,   185),

day7    =   c(15,   11  ,   234),

day8    =   c(9 ,   9   ,   573),

day9    =   c(8 ,   10  ,   335),

day10   =   c(12,   23  ,   446),

day11   =   c(6 ,   12  ,   587),

day12   =   c(9 ,   28  ,   769),

day13   =   c(10,   43  ,   778),

day14   =   c(5 ,   49  ,   1247),

day15   =   c(0 ,   52  ,   1492),

day16   =   c(4 ,   101 ,   1797),

day17   =   c(0 ,   54  ,   977),

day18   =   c(9 ,   141 ,   2313),

day19   =   c(2 ,   160 ,   2651),

day20   =   c(5 ,   172 ,   2547),

day21   =   c(21,   144 ,   3497),

day22   =   c(3 ,   537 ,   3590),

day23   =   c(5 ,   278 ,   3223),

day24   =   c(8 ,   430 ,   3526),

day25   =   c(7 ,   359 ,   4207))

DFtall <- str_sort(DF, numeric = TRUE)) %>% gather(key = Day, value = Value, day1:day25)

print(DFtall)

ggplot(DFtall, aes(Day, Value, fill = group)) + geom_col(position = "dodge")

I am very much new to Rstudio and now am facing an issue when trying to bar graph. Although the above code provides me a bar chart, Output of 

X axis is = day1 , day10,day 11, day 2 

whereas it should be

 day1,day2,day10,day11

 can somebody help me to solve this issue?

I am new to Rstudio and now I am facing issues when I am working on the bar graph. The above code provides me the bar chart as an output 

X-axis is = day1 , day10,day 11, day 2 

but it should give me an output of

day1,day2,day10,day11

Can anyone help me solve this issue?

1 Answer

0 votes
by (36.8k points)

In R Day columns are treated as a categorical column so it has to sort in some order. You can use the factor function to set explicitly. Use the code which will solve your problem.

DFtall <- DF %>% gather(key = Day, value = Value, day1:day25)

DFtall$Day <- factor(DFtall$Day, levels=unique(DFtall$Day))

ggplot(DFtall, aes(Day, Value, fill = group)) + geom_col(position = "dodge")

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Related questions

0 votes
1 answer
asked Apr 26, 2020 in Data Science by blackindya (18.4k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...