ggplot (based on means) with labels

Hi all,
I have an issue with ggplot. Perhaps because I use this package to create plots based mainly on means (in this case avarage A2TB) therefore most of the codes available online are not aaplicable with my request.
Anyway, I managed to create this bar plot

using this code:

ggplot(data=NL.Current.data) +
  geom_bar(aes(monthyear, A2TB), 
           position = "dodge", stat = "summary", fun.y = "mean", fill="#00e5ee") +
  coord_cartesian(ylim = c(60,90))

unfortunately:

  1. I cannot easily convert it to geom_line as after changing geom_bar into geom_line I have following error:
Warning: Ignoring unknown parameters: fill
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
Warning message:
Width not defined. Set with `position_dodge(width = ?)` 
  1. I cannot add labels to each data point
  2. I don't know how to change labels of the monthyear variable (prepared as character to keep them in order: 2017_07 up to 2018_12) to Jul 17 up to Dec 2018
  3. I don't know how to mannually add a horizontal line which is not constant. If the line was constant I would probably add geom_hline statement but I need a line which is on level 80 from 2017_07 to 2017_12 and then on level 85 from 2018_01 to 2018_12

Can you help please?

I also have another ggplot chalange related to plots with means.
I cannot find anywhere how to create one bar chart with nultiple mean scores which would look like this:

Finally, I have issues with using my own fonts in all the plots.
I have already inported them using this:

library(extrafont)
font_import()
font_name <- "My Sans Head Office Light"
grep("My Sans Head Office Light", fonts(), value = TRUE)

But I don't know how to apply this font to all decstiptions (axis, labels, titles etc.)

Can you help me please?

You forgot to share sample data, we can't help you without knowing the structure of your data.

1 Like

Data is required for proper responses, but here are some pointers:

  1. geom_line() uses colour, not fill
  2. geom_text()
  3. There may be more convenient methods, but lubridate::ymd(paste0(monthyear, "_01")), then apply formatting to scale_x_date()
  4. Define a separate dataframe/tibble with two rows defining the two date start/end points and the levels, then apply to geom_hline()

For the font, you can use this in ggplots via a theme function: https://ggplot2.tidyverse.org/reference/theme.html

Lastly, regarding your first plot: bar plots which do not start at zero are a really bad idea. If you do not wish to show the entire range then use e.g. a line plot. (Maybe this was the reason for your question).

1 Like

I wish I could but I still have issues with reprex

No user-supplied code found … so we’ve made some up. You’re welcome!
sprintf("Happy %s!", weekdays(Sys.Date()))
#> [1] "Happy Wednesday!"
Created on 2019-01-30 by the reprex package (v0.2.1)

Thank you. I know I could use theme functions but I don't know how to apply my own fonts.
Yes, the reason for my question was converting the existing plot into a line plot...

No need for use reprex() function in order to share sample data, you can do it with datapasta::df_paste(NL.Current.data).
You have done this before in other question, try to do it again.

For applying your font to all plots try this

library(ggplot2)
library(extrafont)
loadfonts(device = "win") # device = "win" only if you are on windows system

theme_set(
    theme_classic() +
        theme(text=element_text(family="Cloud Calligraphy")) #Use your own font name here
)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()

Created on 2019-01-30 by the reprex package (v0.2.1)

2 Likes

Here is my sample data:

data.frame(stringsAsFactors=FALSE,
             URN = c("9x013122510", "9x013680557", "9x014145627",
                     "9x015094359", "9x013122478", "9x012544645",
                     "9x013122457", "9x015094333", "9x014145611", "9x014963276",
                     "9x015104994", "9x013233865", "9x015094394", "9x013918536",
                     "9x013122546", "9x013122646", "9x013359154", "9x013122574",
                     "9x013122548", "9x015104981", "9x013918605", "9x013122618",
                     "9x013918581", "9x013122341", "9x013122547", "9x013122698",
                     "9x012654879", "9x013703410", "9x013122485", "9x014853449",
                     "9x013122486", "9x014032625", "9x013122697", "9x013122342",
                     "9x012545899", "9x014145624", "9x013233864", "9x013703438",
                     "9x013918532", "9x014635481", "9x012544643", "9x013918584",
                     "9x013122284", "9x012544539", "9x013680544", "9x013918572",
                     "9x013918545", "9x014032615", "9x014635460", "9x015094372"),
   InterviewDate = c("2018-06-04 16:42:00", "2018-05-21 08:30:00",
                     "2018-10-15 13:04:32", "2018-12-17 11:03:42",
                     "2018-06-04 21:25:00", "2017-03-20 20:54:00", "2018-06-05 10:23:00",
                     "2018-12-23 12:01:17", "2018-10-15 19:02:13",
                     "2018-12-10 09:02:05", "2018-12-22 11:01:14", "2018-05-26 15:10:00",
                     "2018-12-20 22:01:27", "2018-05-18 08:59:00", "2018-06-04 14:57:00",
                     "2018-05-31 21:02:00", "2018-05-25 16:05:00",
                     "2018-06-04 12:27:00", "2018-06-04 14:28:00", "2018-12-29 21:01:06",
                     "2018-09-25 14:01:32", "2018-06-03 22:41:00",
                     "2018-10-01 09:01:57", "2017-01-18 08:24:00", "2018-06-04 14:49:00",
                     "2018-05-28 13:46:00", "2017-01-09 11:02:00", "2018-05-20 09:19:00",
                     "2018-06-04 19:45:00", "2018-12-01 12:01:20",
                     "2018-06-04 19:14:00", "2018-10-08 08:02:09", "2018-05-28 14:14:00",
                     "2017-01-12 09:18:00", "2017-01-01 10:33:00",
                     "2018-10-15 13:04:32", "2018-05-26 16:21:00", "2018-05-18 19:16:00",
                     "2018-05-18 10:54:00", "2018-11-16 20:01:54", "2017-02-02 10:51:00",
                     "2018-09-25 12:02:14", "2017-01-15 14:32:00",
                     "2017-01-28 00:11:00", "2018-05-21 09:50:00", "2018-09-26 11:02:27",
                     "2018-05-17 19:40:00", "2018-10-02 11:02:39",
                     "2018-11-24 13:01:10", "2018-12-19 15:01:21"),
              A1 = c(100, 100, 90, 100, 80, 50, 90, 80, 70, 90, 90, 90, 100,
                     100, 100, 90, 90, 100, 100, 100, 100, 100, 100, 100, 80,
                     90, 80, 100, 100, 80, 90, 80, 90, 80, 80, 80, 100, 90, 100,
                     90, 100, 90, 90, 90, 100, 100, 100, 80, 100, 80),
              A2 = c(100, 100, 90, 100, 80, 40, 100, 80, 70, 90, 90, 90, 100,
                     100, 100, 90, 90, 100, 100, 100, 100, 100, 100, 100, 80,
                     90, 80, 100, 100, 80, 90, 80, 90, 90, 90, 80, 100, 90, 100,
                     90, 100, 90, 90, 90, 100, 100, 100, 80, 100, 80),
              B1 = c(100, 100, 90, 100, 90, 60, 90, 80, 80, 90, 100, 90, 90,
                     100, 100, 90, 90, 90, 100, 100, 100, 100, 100, 100, 80,
                     80, 80, 100, 100, 80, 80, 80, 80, 80, 90, 100, 90, 80, 100,
                     80, 80, 90, 80, 90, 100, 100, 100, 90, 80, 80),
              B2 = c(NA, NA, NA, NA, NA, 60, NA, 80, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 70, 90, NA,
                     NA, 80, 80, 80, 80, 90, NA, NA, NA, 80, NA, 80, 100, NA, 80,
                     NA, NA, NA, NA, NA, 80, 80),
              B3 = c(NA, NA, NA, NA, NA, 60, NA, 80, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 80, 80, NA,
                     NA, 80, 80, 80, 80, 90, NA, NA, NA, 70, NA, 80, 100, NA, 80,
                     NA, NA, NA, NA, NA, 90, 80),
              B4 = c(NA, NA, NA, NA, NA, 50, NA, 80, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 80, 80, NA,
                     NA, 80, 90, 80, 80, 90, NA, NA, NA, 70, NA, 80, 100, NA, 80,
                     NA, NA, NA, NA, NA, 80, 80),
              B5 = c(NA, NA, NA, NA, NA, 50, NA, NA, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 70, 80, NA,
                     NA, 80, 90, 80, 90, 80, NA, NA, NA, 70, NA, 90, 100, NA, 80,
                     NA, NA, NA, NA, NA, 90, 80),
              B6 = c(NA, NA, NA, NA, NA, NA, NA, 80, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 10, 90, NA,
                     NA, 80, 80, 80, 60, 70, NA, NA, NA, 70, NA, 100, 100, NA, 80,
                     NA, NA, NA, NA, NA, 80, 80),
            OS21 = c(NA, NA, NA, NA, NA, 50, NA, 80, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 80, 90, NA,
                     NA, 80, 90, 80, 80, 80, NA, NA, NA, 80, NA, 90, 100, NA, 80,
                     NA, NA, NA, NA, NA, 100, 80),
            OS22 = c(NA, NA, NA, NA, NA, 50, NA, 80, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, 80, NA,
                     NA, 80, 90, 80, 80, 90, NA, NA, NA, 90, NA, 90, 40, NA, 80,
                     NA, NA, NA, NA, NA, 80, 80),
              C1 = c(100, 100, 100, 100, 80, 50, 90, 90, 80, 100, 90, 90, 90,
                     100, 100, 100, 90, 100, 100, 100, 100, 100, 100, 100, 80,
                     100, 90, 100, 100, 80, 90, 80, 90, 90, 80, 80, 100, 90, 100,
                     90, 100, 90, 90, 90, 100, 100, 100, 90, 90, 90),
              C2 = c(100, 100, 100, 100, 90, 50, 100, 90, 70, 100, 100, 80,
                     100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
                     100, 80, 100, 80, 100, 100, 80, 90, 80, 90, 90, 100, 80, 100,
                     100, 100, 100, 100, 90, 90, 60, 100, 100, 100, 100, 100, 80),
              C3 = c(100, 100, 100, 100, 90, 60, 100, 90, 80, 100, 90, 90, 90,
                     100, 100, 100, 90, 100, 100, 100, 100, 100, 100, 100, 80,
                     90, 80, 100, 100, 80, 90, 80, 90, 90, 100, 80, 100, 80, 100,
                     90, 100, 90, 90, 80, 100, 100, 100, 90, 100, 80),
              C4 = c(NA, NA, NA, NA, 90, 60, NA, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, NA, NA,
                     NA, 80, NA, 80, NA, NA, 100, 90, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
              C5 = c(NA, NA, NA, NA, 90, 60, NA, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, NA, NA,
                     NA, 80, NA, 80, NA, NA, 100, 80, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
              C6 = c(NA, NA, NA, NA, 90, 60, NA, NA, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, NA, NA,
                     NA, 80, NA, 80, NA, NA, 100, 90, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
              C7 = c(NA, NA, NA, NA, 90, 60, NA, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, NA, NA,
                     NA, 80, NA, 80, NA, NA, 100, 100, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
              C8 = c(NA, NA, NA, NA, 90, 60, NA, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, NA, NA,
                     NA, 80, NA, 80, NA, NA, 100, 90, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
            OS44 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
            OS45 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
              D1 = c(100, 100, 90, 100, 80, 0, 90, NA, 100, NA, NA, 100, NA,
                     100, 100, 90, 80, 90, 100, 100, 100, 100, NA, 100, 0, 100,
                     80, 100, 100, 0, 90, 60, 90, 100, 80, 90, 100, 90, 100, NA,
                     70, NA, NA, 0, 100, 100, 100, 80, NA, 80),
              E1 = c(100, 100, 100, 100, 90, 70, 90, 90, 80, 100, 100, 80, 100,
                     100, 100, 90, 90, 100, 100, 100, 100, 100, 100, 100, 80,
                     80, 80, 100, 100, 80, 90, 80, 90, 90, 90, 80, 100, 80, 100,
                     90, 100, 100, 90, 90, 100, 100, 100, 80, 90, 80),
              E2 = c(100, 100, 100, 100, 90, 80, 50, 90, 90, 100, 100, 90, 100,
                     100, 100, 90, 90, 100, 100, 80, 100, 100, 100, 100, 80,
                     100, 80, 100, 100, 80, 90, 80, 90, 100, 100, 90, 100, 90, 100,
                     90, 100, 90, 80, 70, 100, 100, 100, 80, 100, 80),
              E3 = c(NA, NA, NA, NA, NA, 70, NA, NA, 80, NA, NA, 90, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 100, 80, NA,
                     NA, 70, NA, 80, NA, NA, NA, 90, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 80, NA, 80),
              E4 = c(NA, NA, NA, NA, NA, 60, NA, NA, 90, NA, NA, 80, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, 80, 80, NA,
                     NA, 70, NA, 80, NA, NA, NA, 90, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 70, NA, 80),
              E5 = c(100, 100, NA, 100, 80, NA, NA, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 80, NA, 100, 100, 100, NA, 80, NA, NA, NA,
                     100, NA, 90, NA, 90, NA, NA, NA, NA, NA, NA, NA, NA, 90,
                     NA, NA, NA, 100, NA, NA, NA, NA),
              E6 = c(NA, NA, NA, NA, NA, 50, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, NA, NA,
                     NA, NA, NA, 80, NA, NA, NA, NA, NA, 90, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, 80),
              E7 = c(1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1,
                     1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2),
            OS17 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
              F1 = c(100, 100, 100, 100, 90, 60, 90, 90, 80, 100, 100, 90, 100,
                     100, 100, 90, 90, 90, 100, 100, 100, 100, 100, 100, 80,
                     100, 70, 100, 100, 80, 100, 80, 90, 80, 100, 90, 100, 90, 100,
                     90, 100, 90, 90, 90, 100, 100, 100, 80, 100, 80),
              F2 = c(NA, NA, NA, NA, NA, 80, NA, NA, 100, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, 80, NA,
                     NA, 80, NA, 80, NA, 100, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 90, NA, 80),
              F3 = c(NA, NA, NA, NA, NA, 80, NA, NA, 80, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, 80, NA,
                     NA, 80, NA, 80, NA, 100, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 90, NA, 80),
              F4 = c(NA, NA, NA, NA, NA, 60, NA, NA, 100, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, 80, NA,
                     NA, 80, NA, 80, NA, 100, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 80, NA, 80),
              F5 = c(NA, NA, NA, NA, NA, 60, NA, NA, 60, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, 80, NA,
                     NA, 80, NA, 80, NA, 90, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 80, NA, 70),
              F6 = c(NA, NA, NA, NA, NA, 70, NA, NA, 60, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, 10, NA,
                     NA, 50, NA, 80, NA, 90, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 70, NA, 70),
              F7 = c(NA, NA, NA, NA, NA, 70, NA, NA, 60, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 80, NA, 60, NA,
                     NA, 80, NA, 80, NA, 90, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, 60, NA, 60),
            OS46 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
            OS32 = c(1, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
                     1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 1,
                     1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2),
              G1 = c(100, 100, 0, 100, 0, 60, 90, 0, 90, 100, 100, 60, 0, 100,
                     100, 90, 70, 100, 100, 100, 100, 0, 100, 100, 0, 100, 0,
                     100, 0, 80, 0, 80, 90, 80, 0, 0, 100, 90, 100, 90, 100, 0, 0,
                     90, 100, 100, 100, 0, 0, 0),
              G3 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2,
                     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
              G4 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA),
              G5 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, 80, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                     NA, NA, NA, NA, NA, NA, NA),
              H1 = c(100, 100, 100, 100, 90, 10, 100, 100, 80, 100, 100, 80,
                     100, 100, 100, 90, 90, 100, 100, 100, 100, 100, 100, 100,
                     80, 90, 80, 100, 100, 80, 90, 80, 90, 90, 90, 80, 100, 80,
                     100, 90, 60, 90, 60, 90, 100, 100, 100, 50, 80, 80),
              H3 = c(100, 100, 90, 100, 90, 100, 100, 100, 80, 100, 100, 80,
                     100, 100, 100, 90, 90, 100, 80, 100, 100, 100, 100, 100,
                     80, 90, 80, 100, 100, 80, 90, 100, 100, 90, 100, 90, 100, 80,
                     100, 90, 60, 90, 60, 90, 100, 100, 100, 50, 80, 80),
              H2 = c(100, 100, 90, 100, 90, 100, 100, 100, 90, 100, 100, 80,
                     100, 100, 100, 90, 90, 100, 100, 100, 100, 100, 100, 100,
                     80, 90, 90, 100, 100, 80, 90, 100, 100, 90, 100, 90, 100,
                     80, 100, 90, 80, 90, 60, 90, 100, 100, 100, 50, 90, 80),
              I1 = c(100, 100, 90, 100, 90, 90, 90, 90, 80, 100, 100, 80, 100,
                     90, 100, 80, 90, 100, 80, 90, 100, 100, 100, 100, 80, 90,
                     80, 100, 100, 80, 100, 80, 90, 80, 100, 90, 100, 90, 100,
                     80, 100, 90, 70, 90, 100, 100, 100, 80, 90, 80),
            OS40 = c(2, 1, 2, NA, 1, 1, 1, 1, NA, 1, 1, 2, 1, 1, 1, NA, 2, NA,
                     1, 2, 1, NA, NA, NA, NA, 1, 1, 2, 1, 1, 2, NA, 2, NA, 1,
                     1, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, NA, NA, NA),
       ModelLong = c("ccc", "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )", "ccc (2016 ~ )",
                     "ccc (2016 ~ )", "ccc (2016 ~ )", "aaa (2014 ~ )",
                     "aaa (2014 ~ )", "aaa (2014 ~ )", "aaa (2014 ~ )", "aaa (2014 ~ )",
                     "aaa (2014 ~ )", "aaa (2014 ~ )", "aaa (2014 ~ )",
                     "aaa (2014 ~ )"),
            year = c(2018, 2018, 2018, 2018, 2018, 2017, 2018, 2018, 2018,
                     2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
                     2018, 2018, 2018, 2018, 2018, 2017, 2018, 2018, 2017, 2018,
                     2018, 2018, 2018, 2018, 2018, 2017, 2017, 2018, 2018, 2018,
                     2018, 2018, 2017, 2018, 2017, 2017, 2018, 2018, 2018, 2018,
                     2018, 2018),
           month = c("06", "05", "10", "12", "06", "03", "06", "12", "10",
                     "12", "12", "05", "12", "05", "06", "05", "05", "06",
                     "06", "12", "09", "06", "10", "01", "06", "05", "01", "05",
                     "06", "12", "06", "10", "05", "01", "01", "10", "05", "05",
                     "05", "11", "02", "09", "01", "01", "05", "09", "05", "10",
                     "11", "12"),
          ymonth = c(6, 5, 10, 12, 6, 3, 6, 12, 10, 12, 12, 5, 12, 5, 6, 5, 5,
                     6, 6, 12, 9, 6, 10, 1, 6, 5, 1, 5, 6, 12, 6, 10, 5, 1, 1,
                     10, 5, 5, 5, 11, 2, 9, 1, 1, 5, 9, 5, 10, 11, 12),
              yq = c("2018 Q2", "2018 Q2", "2018 Q4", "2018 Q4", "2018 Q2",
                     "2017 Q1", "2018 Q2", "2018 Q4", "2018 Q4", "2018 Q4",
                     "2018 Q4", "2018 Q2", "2018 Q4", "2018 Q2", "2018 Q2",
                     "2018 Q2", "2018 Q2", "2018 Q2", "2018 Q2", "2018 Q4", "2018 Q3",
                     "2018 Q2", "2018 Q4", "2017 Q1", "2018 Q2", "2018 Q2",
                     "2017 Q1", "2018 Q2", "2018 Q2", "2018 Q4", "2018 Q2", "2018 Q4",
                     "2018 Q2", "2017 Q1", "2017 Q1", "2018 Q4", "2018 Q2",
                     "2018 Q2", "2018 Q2", "2018 Q4", "2017 Q1", "2018 Q3", "2017 Q1",
                     "2017 Q1", "2018 Q2", "2018 Q3", "2018 Q2", "2018 Q4",
                     "2018 Q4", "2018 Q4"),
              hy = c(1, 1, 2, 2, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1,
                     2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1,
                     1, 2, 1, 2, 1, 1, 1, 2, 1, 2, 2, 2),
        halfyear = c(20181, 20181, 20182, 20182, 20181, 20171, 20181, 20182,
                     20182, 20182, 20182, 20181, 20182, 20181, 20181, 20181,
                     20181, 20181, 20181, 20182, 20182, 20181, 20182, 20171, 20181,
                     20181, 20171, 20181, 20181, 20182, 20181, 20182, 20181,
                     20171, 20171, 20182, 20181, 20181, 20181, 20182, 20171, 20182,
                     20171, 20171, 20181, 20182, 20181, 20182, 20182, 20182),
       monthyear = c("2018_06", "2018_05", "2018_10", "2018_12", "2018_06",
                     "2017_03", "2018_06", "2018_12", "2018_10", "2018_12",
                     "2018_12", "2018_05", "2018_12", "2018_05", "2018_06",
                     "2018_05", "2018_05", "2018_06", "2018_06", "2018_12", "2018_09",
                     "2018_06", "2018_10", "2017_01", "2018_06", "2018_05",
                     "2017_01", "2018_05", "2018_06", "2018_12", "2018_06", "2018_10",
                     "2018_05", "2017_01", "2017_01", "2018_10", "2018_05",
                     "2018_05", "2018_05", "2018_11", "2017_02", "2018_09", "2017_01",
                     "2017_01", "2018_05", "2018_09", "2018_05", "2018_10",
                     "2018_11", "2018_12"),
            A2TB = c(100, 100, 100, 100, 0, 0, 100, 0, 0, 100, 100, 100, 100,
                     100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0,
                     100, 0, 100, 100, 0, 100, 0, 100, 100, 100, 0, 100, 100,
                     100, 100, 100, 100, 100, 100, 100, 100, 100, 0, 100, 0)
1 Like

Is this what you want to do?

library(ggplot2)
library(dplyr)

NL.Current.data %>% 
    group_by(monthyear) %>% 
    summarise(A2TB_mean = mean(A2TB)) %>% 
    ggplot(aes(x = monthyear, y = A2TB_mean)) +
    geom_col(fill = "#00e5ee") +
    geom_text(aes(label = round(A2TB_mean, 1)), hjust = 1.5, color = "white") +
    coord_flip()

2 Likes

Also the line plot for the means of A2TB by month, using InterviewDate instead of monthyear

library(dplyr)
library(tibbletime)
library(ggplot2)

NL.Current.data %>% 
    mutate(InterviewDate = as.POSIXct(InterviewDate, tz = 'UTC')) %>%
    arrange(InterviewDate) %>% 
    as_tbl_time(index = InterviewDate) %>% 
    collapse_by("1 month", side = "start", clean = TRUE) %>%
    group_by(InterviewDate) %>% 
    summarise(A2TB_mean = mean(A2TB, na.rm = TRUE)) %>% 
    ggplot(aes(x = InterviewDate, y = A2TB_mean)) +
    geom_line(color = "#00e5ee") +
    scale_x_datetime(date_breaks = "1 month", date_labels = '%Y-%m') +
    theme_classic() +
    theme(axis.text.x = element_text(angle=45, hjust=1, vjust = 1))

2 Likes

Thank you very much. My data frame is called df but when I apply your code:

library(ggplot2)
library(dplyr)

df %>% 
  group_by(monthyear) %>% 
  summarise(A2TB_mean = mean(A2TB)) %>% 
  ggplot(aes(x = monthyear, y = A2TB_mean)) +
  geom_col(fill = "#00e5ee") +
  geom_text(aes(label = round(A2TB_mean, 1)), hjust = 1.5, color = "white") +
  coord_flip()

I have an error:

Error in FUN(X[[i]], ...) : object 'monthyear' not found

monthyear is definitely there. I even exported the df to Excel to double check that.

Anyway, this chart would be very useful but I initially had 2 challenges (perhaps unnecessarily put in one post).
One of them was:
"I also have another ggplot chalange related to plots with means.
I cannot find anywhere how to create one bar chart with nultiple mean scores which would look like this".

Basically I need your chart but with A2TB mean, B1 mean, C1 mean, D1 mean on a side (where the monthyear currently is). I know this might be challenging and not typical for ggplot at x axe represents all means, y axe represents questions (variables).

Brilliant solution but after running your code:

library(dplyr)
library(tibbletime)
library(ggplot2)

df %>% 
  mutate(InterviewDate = as.POSIXct(InterviewDate, tz = 'UTC')) %>%
  arrange(InterviewDate) %>% 
  as_tbl_time(index = InterviewDate) %>% 
  collapse_by("1 month", side = "start", clean = TRUE) %>%
  group_by(InterviewDate) %>% 
  summarise(A2TB_mean = mean(A2TB, na.rm = TRUE)) %>% 
  ggplot(aes(x = InterviewDate, y = A2TB_mean)) +
  geom_line(color = "#00e5ee") +
  scale_x_datetime(date_breaks = "1 month", date_labels = '%Y-%m') +
  theme_classic() +
  theme(axis.text.x = element_text(angle=45, hjust=1, vjust = 1))

I have following error:

Error in FUN(X[[i]], ...) : object 'InterviewDate' not found

Weird :confused:
I am sure this variable is there!

Also, I am struggling in terms of adding labels with one decimal place :frowning_face: to this line plot...

Could you post the result of running names(df)?

Is this what you mean?

library(dplyr)
library(tidyr)
library(ggplot2)

df %>% 
    summarise(A2TB = mean(A2TB),
              B1 = mean(B1),
              C1 = mean(C1),
              D1 = mean(D1, na.rm = TRUE),
              E1 = mean(E1),
              F1 = mean(F1),
              G1 = mean(G1)) %>% 
    gather(variable, mean) %>% 
    ggplot(aes(x = variable, y = mean)) +
    geom_col(fill = "#00e5ee") +
    geom_text(aes(label = round(mean, 1)), hjust = 1.5, color = "white") +
    coord_flip()

2 Likes

You are genious!!!
I don't want to bother too much but is it possible to reverse order of variables on the left?
Manual code rewriting is not helpful as summarise use alphabetical order I suppose.
I know because I failed using this code:

df %>% 
  summarise(G1 = mean(A2TB),
            F1 = mean(B1),
            E1 = mean(C1),
            D1 = mean(D1, na.rm = TRUE),
            C1 = mean(E1),
            B1 = mean(F1),
            A2TB = mean(G1)) %>% 
  gather(variable, mean) %>% 
  ggplot(aes(x = variable, y = mean)) +
  geom_col(fill = "#00e5ee") +
  geom_text(aes(label = round(mean, 1)), hjust = 1.5, color = "white") +
  coord_flip()

Can I also add my own labels to variables with keeping their new order in the plot (for example: A2TB-Europe, B1- UK, C1-Germany, D1-Spain etc)?

Lastly, what should I add to the code to run the plot for all years or hy in once? I know I can use subset function to run the same plot a couple of times but is it possible to run 2 separate plots for different years?

Info required to run your brilliant line plot:

> names(df)
 [1] "URN"           "InterviewDate" "A1"            "A2"            "B1"            "B2"            "B3"            "B4"           
 [9] "B5"            "B6"            "OS21"          "OS22"          "C1"            "C2"            "C3"            "C4"           
[17] "C5"            "C6"            "C7"            "C8"            "OS44"          "OS45"          "D1"            "E1"           
[25] "E2"            "E3"            "E4"            "E5"            "E6"            "E7"            "OS17"          "F1"           
[33] "F2"            "F3"            "F4"            "F5"            "F6"            "F7"            "OS46"          "OS32"         
[41] "G1"            "G3"            "G4"            "G5"            "H1"            "H3"            "H2"            "I1"           
[49] "OS40"          "ModelLong"     "year"          "month"         "ymonth"        "yq"            "hy"            "halfyear"     
[57] "monthyear"     "A2TB"    

Thank you in advance :slight_smile:

You can use reorder() for the sorting issue and you can specify new names at the summarise() level (although, I must say that having non meaningful variable names is considered a bad practice)

library(dplyr)
library(tidyr)
library(ggplot2)

df %>%
    summarise(Europe = mean(A2TB),
              UK = mean(B1),
              Germany = mean(C1),
              Spain = mean(D1, na.rm = TRUE),
              E1 = mean(E1),
              F1 = mean(F1),
              G1 = mean(G1)) %>% 
    gather(country, mean) %>% 
    ggplot(aes(x = reorder(country, desc(mean)), y = mean)) +
    geom_col(fill = "#00e5ee") +
    geom_text(aes(label = round(mean, 1)), hjust = 1.5, color = "white") +
    labs(x = 'Country') +
    coord_flip()

And for the last question you can use facet_wrap() for making multiple plots (facets) at once for each unique value of a categorical variable.

1 Like

oh, wow! Thank you. Final questions if you don't mind:

  1. Were did you specify "country"? This is only and example and variables could be called "scores" etc.
  2. The plot is sorted by mean. Can I manually specify that (for example: Europe, Germany, UK...)?
  3. Is it possible to change variable names to full names with spaces (for example "United Kingdom")?
  1. You can call your axis the way you want (this is exactly why you should be using meaningful variable names instead of A1, B1, etc).
  2. One way to manually specify the order is changing your variable class to factor and specifying levels factor(country, levels = c('Europe', 'Germany', 'UK')).
  3. It is possible, but not recommended summarise(`United Kingdom` = mean(A2TB)