1

I'm new in R and ggplot2. Currently I have a plot like this:

http://imgur.com/lJFXVfZ.png

And I want something like that:

http://imgur.com/czXg0Yb.png

Unfortunately I have no idea, how to split characters on x-axis and assign them a different color.

This is my code:

attach(trustData)

ag <- aggregate(trustData, by = list(type = face_category_and_gender2_ZAUFANIE), mean)

plot <- ggplot(data = ag, aes(x = type, y = response)) + list(geom_point(colour = 'red'))

plot

As new member, I can't put here more than 2 links, so example of already aggregated data is in comments

Thanks!

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 1
    Please show a small reproducible example data. You have to create a group column to get that. If you show some example data, it will be easier for us to help – akrun Jan 22 '17 at 07:26
  • http://imgur.com/qdDqhGQ.png Here you go –  Jan 22 '17 at 07:31
  • Ok, I'm going to update. K - Woman M - Men –  Jan 22 '17 at 07:39
  • The image you showed, is it the `ag` data or the original 'trustData`? – akrun Jan 22 '17 at 07:47
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jan 22 '17 at 07:50
  • Thank you very much! Is there any possibility to make red and blue point in one line? Like in 2nd picture? –  Jan 22 '17 at 08:00

1 Answers1

0

We can separate the 'type' column and then do the plotting

library(dplyr)
library(tidyr)
library(ggplot2)
ag %>% 
    separate(type, into = c("val", "Type"), "(?=[A-Z])", remove = FALSE) %>%
    mutate(val = factor(val, levels = sort(unique(as.numeric(val)))),
            Type = factor(Type, levels =c("M", "K"), labels = c("Men", "Woman"))) %>%
    ggplot(., aes(x = val, y = response, col = Type)) + 
                geom_point()

Or instead of factor use the case_when from dplyr

ag %>%
   separate(type, into = c("val", "Type"), "(?=[A-Z])", remove = FALSE) %>% 
   mutate(val = factor(val, levels = sort(unique(as.numeric(val)))),
          Type = case_when(.$Type == "M" ~ "Men", TRUE ~ "Woman")) %>%
   ggplot(., aes(x = val, y = response, col = Type)) + 
                geom_point()

enter image description here

data

ag <- structure(list(type = c("12K", "12M", "15K", "15M", "1K", "1M", 
"4K", "4M", "7K", "7M", "9K", "9M"), response = c(41.83, 42.45, 
40.61, 40.69, 64.59, 57.88, 61.2, 54.71, 49.23, 48.24, 44.27, 
45.09)), .Names = c("type", "response"), row.names = c(NA, -12L
), class = "data.frame")
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662