0
votes

I'm having trouble merging a ggplot2 legend that assigns color and shape based on different variables. One variable assigns the shape, whereas another assigns the color. However, the legend is split into two (one with the shape, one with the colors) is there a simple way to merge the two?

Sample Data:

       ID   EFFRENT      SQFT Layers FLOORPLAN
1   10496  3369.196  817.9767   1 BR     1_1_0
2   10496  4635.274 1242.8750    2BR     2_2_0
3   10496  5621.419 1656.6250    3BR     3_3_0
4   10752  2180.625  775.0000   1 BR     1_1_0
5   10752  2744.991 1100.8620    2BR     2_2_0
6   11127  2335.705  692.0893   1 BR     1_1_0
7   11127  2730.000  659.0000    2BR     2_1_0
8   11127  3272.705  860.2480    2BR     2_2_0
9   11127  4832.667 1253.0000    3BR     3_2_0
10   1163  2540.833  700.0000   1 BR     1_1_0
11   1163  2734.767  820.0000   1 BR   1_1_0_D
12   1163  2981.783  854.0000   1 BR   1_1_0_L
13   1163  2987.167  963.0000    2BR     2_2_0
14   1163  3193.043 1292.0000    2BR   2_2_0_L

ggplot code:

    Z <- ggplot(dataset, aes(x=SQFT,y=EFFRENT)) +  geom_point() 

# Number of unique floorplans and colorramp
a <-  length(factor(levels(dataset$FLOORPLAN)))
clr <- c("#814d35","#576df7","#1b9e13","#e166eb","#01dc82","#d13fc1","#9dd755","#7b2ca2","#dcc821","#0277e3","#bcb900","#c982ff","#7edb71","#9d0088","#01a055","#e70082","#009763","#ef274a","#02c0fc","#da5601","#0c9bff","#c59000","#554698","#dac751","#9a0f77","#9b8400","#ff95ee","#4c5804","#dbafff","#b55300","#aebaff","#e6402a","#00886b","#ff4874","#38958a","#ff643a","#b0d094","#af0058","#dfc38d","#89306c","#946100","#ff67a8","#8a3b19","#ffa3cb","#b3003e","#8b628e","#ff665f","#80413e","#ff9886","#ff99a4")

#Add Shapes and Color
Z2 <-   Z + aes(shape = factor(dataset$Layers)) + 
     geom_point(aes(colour = factor(dataset$FLOORPLAN)),  size = 6) + scale_colour_manual( name="Floorplan",  values=clr) 


#Modify Axis Titles
Z3 <-Z2 + theme(axis.title.x = element_text(face="bold", colour="#000000", size=20),
           axis.text.x  = element_text(angle=90, vjust=0.5, size=16)) + theme(axis.title.y = element_text(face="bold", colour="#000000", size=20),
           axis.text.y  = element_text(angle=90, vjust=0.5, size=16))   +  labs(x="Square Footage", y="Effective Rent")

This yields a legend like so:

Legend

Where I'm hoping to get all of the floor plans in one entry, with their respective color and shape (all 0_* the same shape, all 1_* the same shape, etc)

I know this shouldn't be hard but I can't seem to find a solution

1

1 Answers

0
votes

The solution is to collapse the levels in the FLOORPLAN variable of your dataset. The fct_collapse function from the forcats package easily does this, but this is a lot to write so I'm only putting the first couple of rows to get you on your way.

library(forcats)
dataset$FLOORPLAN <- factor(dataset$FLOORPLAN) %>%
                     fct_collapse(0_* = c("0_1_0", "0_1_0_D", etc.),
                                  1_* = c("1_1_0", "1_1_0_D", etc.),
                                  etc...)

Your clr vector of colours will need to be amended accordingly, since there would be only 4 colours to apply. I'll leave it up to you to choose which colours you want.

In regards to the chart itself, in your call of the geom_point layer, remove the factor() function call since it's already used above:

geom_point(aes(colour = FLOORPLAN),  size = 6)

The rest of your code should work as is. I'm confused as to the purpose of this line:

 a <-  length(factor(levels(dataset$FLOORPLAN)))

The a object doesn't appear to be used for anything else.