简体   繁体   中英

Charting in R - Bar chart and Line chart

I am an excel user and trying to use R now to replicate the work in excel. My dataset is as below:

Time Utilisation ExpensedFTE 1/04/2019 0.7625 0.8 1/03/2019 0.813 0.923
1/02/2019 0.88 0.8 1/06/2019 1 1 1/01/2019 0.585714286 1

I am trying to create a line chart with time line to show the pattern in Utilisation and expensed FTE. I can create a bar chart with one variable only (either Utilisation or ExpensedFTE) using the code below. I cannot create a bar chart with both the variables.

ggplot(FindataCOmp,aes(x=Time,y=Count_of_FTE_Actual))+geom_bar(stat='identity')

I also failed to create a line chart with single or multiple variable.

Can I please get some help?

If you want to create a line chart, you can use geom_line() instead of geom_bar() . Also, for the plot you are trying to create, it's usually good to put the data into long format before plotting. Here's an example with the data you've provided:

library(ggplot2)
library(dplyr)

df <- data.frame(Time = c("1/04/2019", "1/03/2019", "1/02/2019", "1/06/2019", "1/01/2019"),
                 Utilisation = c(0.7625, 0.813, 0.88, 1, 0.585714286),
                 ExpensedFTE = c(0.8, 0.923, 0.8, 1, 1))

df %>%
  gather(group, value, Utilisation:ExpensedFTE) -> graph_data

ggplot(graph_data, aes(x = Time, y = value, group = group)) + 
  geom_line(aes(color = group))

在此处输入图片说明

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