0

-How can I scale the Y- and X- axis to 0,30 and 0,50 respectively and plot the data point according to the scale?

-How do I change the color of groups 1, 2 and 3 to purple, orange and yellow?

 library(ggplot2)
 library(plotly)

ID <- c("Group 1", "Group 1", "Group 1", "Group 2", "Group 2", "Group 2", "Group 3", "Group 3", "Group 3", "Group 4", "Group 4", "Group 4")
area <- c("Area 1", "Area 1", "Area 1","Area 2", "Area 2", "Area 2", "Area 3", "Area 3", "Area 3", "Area 4", "Area 4", "Area 4")
x <- c(1.0, 10.25, 50.0, 2.0, 5.0, 30.0, 5.0, 9.0, 10.0, 11.0, 23.0, 40.0) 
y <- c(1.0, 3.0, 5.0, 20.0, 10.0, 23.0, 25.0, 19.1, 5.0, 15.0, 8.0, 4.0)

df <- cbind(ID, area, x, y)
df <- as.data.frame(df)
df

p <- ggplot(df, aes(x=x, y=y)) + geom_polygon(aes(fill=factor(ID), group=area))

p <- ggplotly(p)
p

enter image description here

I have tried to play around with scale_x_continuous, scale_fill_manual, and scale_fill_identity but it doesn`t seem to do anything.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
Nivel
  • 629
  • 4
  • 12
  • 2
    Can you show the code that you tried? BTW use `df <- data.frame(ID, area, x, y, stringsAsFactors = FALSE)` to create your data, not `cbind` first. That saves you some trouble (look at the labels of your axis for example). – markus Feb 17 '19 at 18:18
  • 2
    As @markus said, you're running into issues due to the use of `cbind`, which is creating a matrix where all the values have the same type. In this case, that type is character, but you probably want those `x` and `y` values treated as numeric values, not characters. If you switch over to markus' suggestion, you'll have a better time with those `scale_*` functions. – Jota Feb 17 '19 at 18:34

2 Answers2

2

First, convert the x and y columns to number (they are factors in the data). Then, set the scale limits using scale_x_continuous and scale_y_continuous. Finally, use scale_fill_manual to change the colors (since you have a fourth group, I gave it another color).

library(tidyverse)
df <- df %>% mutate(x = parse_number(x),
                        y = parse_number(y))

p <- ggplot(df, aes(x=x, y=y)) + geom_polygon(aes(fill=factor(ID), group=area)) + 
    scale_x_continuous(limits = c(0,50)) + 
    scale_y_continuous(limits = c(0,30)) + 
    scale_fill_manual(values = c("purple", "orange", "yellow", "gray40"))
ggplotly(p)
Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21
1

From the above comments I could solve it like this.

library(ggplot2)
library(plotly)

ID <- c("Group 1", "Group 1", "Group 1", "Group 2", "Group 2", "Group 2", "Group 3", "Group 3", "Group 3", "Group 1", "Group 1", "Group 1")
area <- c("Area 1", "Area 1", "Area 1","Area 2", "Area 2", "Area 2", "Area 3", "Area 3", "Area 3", "Area 4", "Area 4", "Area 4")
colours <- c("Purple", "Purple", "Purple", "Green", "Green", "Green", "yellow", "yellow", "yellow", "Purple", "Purple", "Purple")
x <- c(1.0, 10.25, 50.0, 2.0, 5.0, 30.0, 5.0, 9.0, 10.0, 11.0, 23.0, 40.0) 
y <- c(1.0, 3.0, 5.0, 20.0, 10.0, 23.0, 25.0, 19.1, 5.0, 15.0, 8.0, 4.0)

df <- data.frame(ID, area, colours, x, y, stringsAsFactors = FALSE )

###Get colours
colours_poly <- setNames(df$colours, df$ID)
colours_poly

p <- ggplot(df, aes(x=x, y=y)) + geom_polygon(aes(fill=factor(ID), group=area)) + 
     scale_x_continuous(limits = c(0,50)) + 
 scale_y_continuous(limits = c(0,30)) +
 scale_fill_manual(values = colours_poly)


p <- ggplotly(p)
p

Thanks a lot! enter image description here

Nivel
  • 629
  • 4
  • 12