Setting legend with Plotly - r

I am working in R with Plotly. Below you can see my bar chart and data.
t_df3<-data.frame(structure(list(deciles = c(0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002,
0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001,
0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01,
0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008,
0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01), variable.x = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Food",
"Alcoholic Beverages", "Clothing", "Housing Water", "Furnishings",
"Health", "Transport", "Communication", "Recreation", "Education",
"Restaurants", "Miscellaneous Goods"), class = "factor"), value.x = c(958.823102803738,
1292.9, 1575.2, 1807, 1911.8,
2041.2, 2376.83, 2723, 3161.9,
4130.448, 120.0, 304, 246.4,
249.8, 285, 382.1, 494.5,
691.6, 787, 948.8, 11.9,
22.9085309734513, 62, 77.3, 201.184778761062,
239.961132743363, 330, 588.19178761062, 766.9,
2117.2, 46, 91.0239292035398, 166.0,
329.5, 525, 772.3, 1060.3112920354,
1297.9, 1680, 3963.0, 106.8,
175.2, 228, 295.961379310345, 300.2,
404.8, 447, 496.528551724138, 731.2,
1916.6995862069, 68, 71.9, 111.1,
154.9, 201, 253.49, 248.205798165138,
247.0, 421, 850.106642201835, 19.2,
14.9, 40, 96.0269734513274, 200.4,
354.0, 457, 745.2, 929.6,
2654.9, 4.9, 12.6, 50.6,
77.0251034482759, 168., 259.1, 364.2,
571.067586206897, 828.672, 1452.7, 6.75,
4.5, 5.41241379310345, 25.2, 46.1,
68.2, 125.2, 104.550620689655, 258.9,
951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5, 14.1,
44.5, 79.2, 119.3, 216.31,
316.8, 577.3, 901.7, 1897.1,
70.5037168141593, 97.10, 136.4, 189.5,
250.2, 333.9, 439.5, 583.7,
725.7, 1283.5), color.x = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("blue",
"cyan", "darkgreen", "red", "brown", "chartreuse", "green", "purple",
"gold", "tomato", "darkturquoise", "forestgreen"), class = "factor"),
deciles = c(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002,
0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001,
0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01,
0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008,
0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("Food", "Alcoholic Beverages",
"Clothing", "Housing Water", "Furnishings", "Health", "Transport",
"Communication", "Recreation", "Education", "Restaurants",
"Miscellaneous Goods"), class = "factor"), value = c(145.111841584158,
195.676277227723, 238.405544554455, 273.52, 289.35,
308.935841584158, 359.719128712871, 412.1, 478.5,
625.117306930693, 96.9022702702703, 245.5, 198.9,
201.624648648649, 230.688432432432, 308.3, 399.0,
558.103135135135, 635.512216216216, 765.6, 11.9,
22.9085309734513, 62.3668672566372, 77.3, 201.1,
239.961132743363, 330.529486725664, 588.1, 766.9,
2117.21543362832, 46.402407079646, 91.0, 166.0,
329.568637168142, 525.05182300885, 772.307681415929, 1060.3112920354,
1297.90619469027, 1680.52311504425, 3963.01847787611, 106.808275862069,
175.229793103448, 228.774620689655, 295.961379310345, 300.238344827586,
404.881655172414, 447.85324137931, 496.528551724138, 731.241931034483,
1916.9, 68.8187889908257, 71.9220550458716, 111.1,
154.5, 201.2, 253.4, 248.2,
247.1, 421.0, 850.106642201835, 19.2,
14.94, 40.8, 96.0269734513274, 200.4,
354.09, 457.8, 745.21614159292, 929.6,
2654.1, 4.97, 12.6554482758621, 50.6,
77.0, 168.119172413793, 259.120551724138, 364.27,
571.07, 828.672, 1452.78786206897, 6.75310344827586,
4.55834482758621, 5.41241379310345, 25.2513103448276, 46.1428965517241,
68.2262068965517, 125.256827586207, 104.550620689655, 258.918620689655,
951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5453793103448, 14.1020689655172,
44.5, 79.2, 119.3, 216.3,
316.8, 577.3, 901.7, 1897.1,
70.5, 97.10, 136.421309734513, 189.527575221239,
250.2, 333.986336283186, 439.591433628319, 583.765805309735,
725.7, 1283.5), color = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("blue", "cyan", "darkgreen",
"red", "brown", "chartreuse", "green", "purple", "gold",
"tomato", "darkturquoise", "forestgreen"), class = "factor")), class = "data.frame", row.names = c(NA,
-120L)))
Above is artificial data and below is code for bar chart
library(plotly)
plt <- plot_ly(t_df3) %>%
add_trace(x = ~deciles, y = ~value.x, type = 'bar',name = 'Left-scale',marker = list(color = ~color.x), name = ~variable.x) %>%
add_trace(x = ~deciles, y = ~value, type = 'bar', name = 'Right-scale',marker = list(color = ~color), name = ~variable) %>%
layout(
xaxis = list(title = '',font = t_8),
yaxis = list(title = ''),
legend = list(x = 0.01, y = 0.99,font = t_10),
barmode = 'bar'
)
plt
On the left side, you can see a legend, but I am not satisfied with this legend and I want to change this legend, with a legend with the structure of the data (e.g. Food, Alcoholic Beverages, etc.). The structure is same for left and also right bars.So can anybody help with this ?

Would this be suitable?
If so, then this is how you can make this plot.
First, I melted the data.
t_df4 <- pivot_longer(t_df3, cols = c(value, value.x),
names_to = "group") %>%
mutate(group = ifelse(group == "value", "right_side",
"left_side"))
Then I plotted.
plot_ly(t_df4, x = ~list(deciles, group), y = ~value,
color = ~variable, colors = ~as.character(color), type = "bar") %>%
layout(barmode = "stack",
xaxis = list(title = ''),
yaxis = list(title = ''),
legend = list(x = 0.01, y = 0.99))

Related

Setting bars and legend properly

I am working in R with Plotly. Below you can see my bar chart and data.
t_df3<-data.frame(structure(list(deciles = c(0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002,
0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001,
0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01,
0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008,
0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01), variable.x = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Food",
"Alcoholic Beverages", "Clothing", "Housing Water", "Furnishings",
"Health", "Transport", "Communication", "Recreation", "Education",
"Restaurants", "Miscellaneous Goods"), class = "factor"), value.x = c(958.823102803738,
1292.9, 1575.2, 1807, 1911.8,
2041.2, 2376.83, 2723, 3161.9,
4130.448, 120.0, 304, 246.4,
249.8, 285, 382.1, 494.5,
691.6, 787, 948.8, 11.9,
22.9085309734513, 62, 77.3, 201.184778761062,
239.961132743363, 330, 588.19178761062, 766.9,
2117.2, 46, 91.0239292035398, 166.0,
329.5, 525, 772.3, 1060.3112920354,
1297.9, 1680, 3963.0, 106.8,
175.2, 228, 295.961379310345, 300.2,
404.8, 447, 496.528551724138, 731.2,
1916.6995862069, 68, 71.9, 111.1,
154.9, 201, 253.49, 248.205798165138,
247.0, 421, 850.106642201835, 19.2,
14.9, 40, 96.0269734513274, 200.4,
354.0, 457, 745.2, 929.6,
2654.9, 4.9, 12.6, 50.6,
77.0251034482759, 168., 259.1, 364.2,
571.067586206897, 828.672, 1452.7, 6.75,
4.5, 5.41241379310345, 25.2, 46.1,
68.2, 125.2, 104.550620689655, 258.9,
951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5, 14.1,
44.5, 79.2, 119.3, 216.31,
316.8, 577.3, 901.7, 1897.1,
70.5037168141593, 97.10, 136.4, 189.5,
250.2, 333.9, 439.5, 583.7,
725.7, 1283.5), color.x = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("blue",
"cyan", "darkgreen", "red", "brown", "chartreuse", "green", "purple",
"gold", "tomato", "darkturquoise", "forestgreen"), class = "factor"),
deciles = c(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004,
0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003,
0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001, 0.002,
0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.001,
0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01,
0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008,
0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007,
0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006,
0.007, 0.008, 0.009, 0.01, 0.001, 0.002, 0.003, 0.004, 0.005,
0.006, 0.007, 0.008, 0.009, 0.01), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("Food", "Alcoholic Beverages",
"Clothing", "Housing Water", "Furnishings", "Health", "Transport",
"Communication", "Recreation", "Education", "Restaurants",
"Miscellaneous Goods"), class = "factor"), value = c(145.111841584158,
195.676277227723, 238.405544554455, 273.52, 289.35,
308.935841584158, 359.719128712871, 412.1, 478.5,
625.117306930693, 96.9022702702703, 245.5, 198.9,
201.624648648649, 230.688432432432, 308.3, 399.0,
558.103135135135, 635.512216216216, 765.6, 11.9,
22.9085309734513, 62.3668672566372, 77.3, 201.1,
239.961132743363, 330.529486725664, 588.1, 766.9,
2117.21543362832, 46.402407079646, 91.0, 166.0,
329.568637168142, 525.05182300885, 772.307681415929, 1060.3112920354,
1297.90619469027, 1680.52311504425, 3963.01847787611, 106.808275862069,
175.229793103448, 228.774620689655, 295.961379310345, 300.238344827586,
404.881655172414, 447.85324137931, 496.528551724138, 731.241931034483,
1916.9, 68.8187889908257, 71.9220550458716, 111.1,
154.5, 201.2, 253.4, 248.2,
247.1, 421.0, 850.106642201835, 19.2,
14.94, 40.8, 96.0269734513274, 200.4,
354.09, 457.8, 745.21614159292, 929.6,
2654.1, 4.97, 12.6554482758621, 50.6,
77.0, 168.119172413793, 259.120551724138, 364.27,
571.07, 828.672, 1452.78786206897, 6.75310344827586,
4.55834482758621, 5.41241379310345, 25.2513103448276, 46.1428965517241,
68.2262068965517, 125.256827586207, 104.550620689655, 258.918620689655,
951.36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15.5453793103448, 14.1020689655172,
44.5, 79.2, 119.3, 216.3,
316.8, 577.3, 901.7, 1897.1,
70.5, 97.10, 136.421309734513, 189.527575221239,
250.2, 333.986336283186, 439.591433628319, 583.765805309735,
725.7, 1283.5), color = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("blue", "cyan", "darkgreen",
"red", "brown", "chartreuse", "green", "purple", "gold",
"tomato", "darkturquoise", "forestgreen"), class = "factor")), class = "data.frame", row.names = c(NA,
-120L)))
Below you can see code in Plotly that produce the chart shown below
library(plotly)
plt <- plot_ly(t_df3) %>%
add_trace(x = ~deciles, y = ~value.x, type = 'bar',name = 'Left-scale',marker = list(color = ~color.x), name = ~variable.x) %>%
add_trace(x = ~deciles, y = ~value, type = 'bar', name = 'Right-scale',marker = list(color = ~color), name = ~variable) %>%
layout(
xaxis = list(title = ''),
yaxis = list(title = ''),
legend = list(x = 0.01, y = 0.99),
barmode = 'bar'
)
Now I want to change the legend on this bar chart that is shown on the left side and to have bar chart as the bar chart shown below.
So can anybody help me how to solve this problem ?
Here is a hack solution. A better one might exists.
library(plotly)
plt <- plot_ly(t_df3) %>%
add_trace(x = ~deciles, y = ~value.x, type = 'bar', marker = list(color = ~color.x), name = ~variable.x) %>%
add_trace(x = ~deciles + 0.0004, y = ~value, type = 'bar', marker = list(color = ~color), name = ~variable) %>%
layout(
xaxis = list(title = ''),
yaxis = list(title = ''),
legend = list(x = 0.01, y = 0.99),
barmode = 'bar'
)
plt <- layout(p = plt, barmode = "stack")
plt
Second trace have an artificial x increase, traces are stacked in layout (info on hover has to be corrected).

ggplot2 - customize two-factor legend

I am using ggplot2 to plot monthly vertical profiles of soil moisture in two sites, for both observed and modeled data.
I am using interaction to add colours to both factors (month and type). I am also creating two different manual color palettes with the colors I need. This is how to to reproduce the plot:
library(ggplot2)
df1<- structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L), .Label = c("IL_Shabbona_5_NNE", "ME_Limestone_4_NNW",
"ME_Old_Town_2_W", "MI_Chatham_1_SE", "MI_Gaylord_9_SSW", "MN_Goodridge_12_NNW",
"MN_Sandstone_6_W", "NY_Ithaca_13_E", "NY_Millbrook_3_W", "WI_Necedah_5_WNW"
), class = "factor"), month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L), depth = c(5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100), value = c(0.38,
0.4, 0.37, 0.32, 0.29, 0.3, 0.24, 0.28, 0.24, 0.26, 0.32, 0.39,
0.13, NaN, 0.13, 0.12, 0.1, 0.1, 0.06, 0.07, 0.09, 0.1, 0.12,
0.13, 0.39, 0.39, 0.37, 0.35, 0.33, 0.31, 0.27, 0.29, 0.27, 0.28,
0.34, 0.38, 0.1, NaN, 0.12, 0.11, 0.09, 0.09, 0.05, 0.06, 0.09,
0.09, 0.11, 0.11, 0.39, 0.41, 0.38, 0.35, 0.34, 0.32, 0.29, 0.33,
0.31, 0.3, 0.34, 0.36, 0.1, NaN, 0.1, 0.1, 0.09, 0.08, 0.05,
0.05, 0.08, 0.08, 0.1, 0.1, 0.32, 0.31, 0.33, 0.34, 0.36, 0.34,
0.29, 0.33, 0.32, 0.31, 0.32, 0.33, 0.06, 0.06, 0.07, 0.06, 0.06,
0.05, 0.03, 0.03, 0.04, 0.05, 0.06, 0.06, 0.4, 0.4, 0.41, 0.41,
0.45, 0.47, 0.43, 0.4, 0.39, 0.38, 0.38, 0.4, 0.05, 0.05, 0.05,
0.06, 0.05, 0.05, 0.04, 0.04, 0.05, 0.05, 0.06, 0.05, 0.35, 0.35,
0.36, 0.33, 0.29, 0.28, 0.27, 0.26, 0.26, 0.28, 0.3, 0.36, 0.35,
0.35, 0.36, 0.33, 0.29, 0.28, 0.27, 0.27, 0.27, 0.28, 0.3, 0.35,
0.34, 0.35, 0.35, 0.34, 0.3, 0.29, 0.28, 0.28, 0.28, 0.29, 0.3,
0.34, 0.28, 0.29, 0.3, 0.32, 0.31, 0.3, 0.29, 0.29, 0.29, 0.3,
0.3, 0.29, 0.26, 0.27, 0.27, 0.29, 0.29, 0.29, 0.28, 0.28, 0.28,
0.29, 0.29, 0.28, 0.38, 0.38, 0.39, 0.38, 0.31, 0.3, 0.29, 0.29,
0.3, 0.31, 0.35, 0.39, 0.36, 0.36, 0.37, 0.37, 0.31, 0.31, 0.29,
0.3, 0.3, 0.31, 0.33, 0.37, 0.37, 0.37, 0.37, 0.38, 0.32, 0.32,
0.31, 0.31, 0.31, 0.32, 0.33, 0.37, 0.31, 0.32, 0.32, 0.34, 0.33,
0.32, 0.31, 0.31, 0.32, 0.32, 0.31, 0.3, 0.27, 0.28, 0.28, 0.29,
0.31, 0.3, 0.3, 0.29, 0.3, 0.3, 0.3, 0.28), type = rep(c("observed","modeled"), each=120)), class = "data.frame", row.names = c(NA,
-240L))
# Create blue and red palettes
mypal.blue <- colorRampPalette(RColorBrewer::brewer.pal(6,"PuBu"))
mypal.red <- colorRampPalette(RColorBrewer::brewer.pal(6,"YlOrRd"))
# Plot
ggplot(df1, aes(x=value, y=-depth, colour=interaction(as.factor(month),type))) +
geom_path(size=1) + geom_point(size=0.7) +
facet_wrap(~ site, nrow=3) +
theme_bw(base_size=20) +
scale_colour_manual(values=c(mypal.blue(12),mypal.red(12))) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.title=element_blank()) + theme(legend.position = c(0.75, 0.13))
However, the legend is a complete mess.
I would like to create two separate legends, loosely based on this example.
one showing orange for observed and blue for modeled
the other one showing the actual color gradients and the months (ideally with the first letter instead of numbers)
How to create such legends?
Updated Answer
It just hit me that there is a relatively straightforward way to hack the legend to get pretty close to what you want. We relabel the legend labels and add a title. The hacky part is that you have to fiddle with the legend title spacing, legend key width, and text size to get the titles lined up over the legend keys.
With all those lines and colors and the complicated legend, the plot seems very busy and difficult to interpret beyond showing that the model doesn't fit the data very well, so maybe it would still be better to consider one of the other options in my or #neilfws's answer. In addition, because the legend title is manually hardcoded, it's not linked to the aesthetic mapping and you therefore have to be careful that "Modeled" and "Observed" are in the right order above the legend keys.
ggplot(df1, aes(x=value, y=-depth, colour=interaction(as.factor(month),type))) +
geom_path(size=1) + geom_point(size=0.7) +
facet_wrap(~ site, nrow=3) +
theme_bw(base_size=20) +
scale_colour_manual(values=c(mypal.blue(12),mypal.red(12)),
labels=rep(month.abb, 2)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.title=element_text(size=rel(0.6)),
legend.text=element_text(size=rel(0.5)),
legend.key.width=unit(1.1,"cm")) +
labs(colour="Modeled Observed")
Original Answer
AFAIK, there's no way to generate two separate legends for a single aesthetic within the normal ggplot workflow. In this case, that means you can have only a single color legend. Probably you could hack two different color legends by manipulating the underlying ggplot grob structure.
Another option would be to use two different aesthetics. The example below uses linetype to distinguish modeled and observed, but it doesn't provide as much constrast as the two different color sets.
library(tidyverse)
ggplot(df1 %>%
mutate(month=factor(month.abb[month], levels=month.abb)),
aes(x=value, y=-depth, linetype=type, colour=month)) +
geom_path(size=1) + geom_point(size=0.7) +
facet_wrap(~ site, nrow=3) +
theme_bw(base_size=20) +
scale_colour_manual(values=mypal.red(12)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.title=element_blank())
For reference, here's what your original code produces (minus the change in legend position):
Another option would be to facet by month in addition to type. This takes up more space, but makes it easier to see both the month trend and the difference between modeled and observed.
ggplot(df1 %>%
mutate(month=factor(month.abb[month], levels=month.abb)),
aes(x=value, y=-depth, colour=type)) +
geom_path(size=1) + geom_point(size=0.7) +
facet_grid(month ~ site) +
theme_classic() +
theme(panel.background=element_rect(colour="grey50", fill=NA))
Looking at your data, it seems to me that what you want to visualize can be expressed something like this:
"How do observed values compare to modelled values at different depths, for each site, through time?"
So I would approach the chart differently: plot value versus month, color by type and use facets for site and depth.
library(tidyverse)
df1 %>%
mutate(Month = factor(month.abb[month],
levels = month.abb)) %>%
ggplot(aes(Month, value)) +
geom_point(aes(color = type)) +
facet_grid(depth~site) +
theme_bw()
It's now immediately apparent that the modeled values for site IL_Shabbona_5_NNE are closer to the observed, and more so at shallower depth.

R plot color legend by factor

Using R 3.3.1 in Windows 10. I'm making an x-y plot from 95 rows of data. The data are in 6 different groupings (a factor called "group"). The plot itself is easy enough, but I can't get the legend to properly account for the factor and color correctly.
Here's the data in a variable v1:
v1 <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("F9", "T26", "W37",
"W40", "W41", "W42"), class = "factor"), point = c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L), x = c(-7.064, -5.1681,
-6.4866, -2.7522, -4.6305, -4.2957, -3.7552, -4.9482, -5.6452,
-6.0302, -5.3244, -3.9819, -3.8123, -5.3085, -5.6096, -6.4557,
-5.2549, -3.4893, -3.5909, -2.5546, -3.7247, -5.1733, -3.3451,
-2.8993, -2.6835, -3.9495, -4.9649, -2.8438, -4.6926, -3.4768,
-3.1221, -4.8175, -4.5641, -3.549, -3.08, -2.4153, -2.9882, -3.4045,
-4.6394, -3.3404, -2.6728, -3.3517, -2.6098, -3.7733, -4.051,
-2.9385, -4.5024, -4.59, -4.5617, -4.0658, -2.4986, -3.7559,
-4.245, -4.8045, -4.6615, -4.0696, -4.6638, -4.6505, -3.7978,
-4.5649, -5.7669, -4.519, -3.8561, -3.779, -3.0549, -3.1241,
-2.1423, -3.2759, -4.224, -4.028, -3.3412, -2.8832, -3.3866,
-0.1852, -3.3763, -4.317, -5.3607, -3.3398, -1.9087, -4.431,
-3.7535, -3.2545, -0.806, -3.1419, -3.7269, -3.4853, -4.3129,
-2.8891, -3.0572, -5.3309, -2.5837, -4.1128, -4.6631, -3.4695,
-4.1045), y = c(7.76, 0.72, 4.1, 1.36, 0.13, -0.02, 0.13, 0.42,
1.49, 2.64, 1.01, 0.08, 0.22, 1.01, 1.53, 4.39, 0.99, 0.56, 0.43,
2.31, 0.31, 0.59, 0.62, 1.65, 2.12, 0.1, 0.24, 1.68, 0.09, 0.59,
1.23, 0.4, 0.36, 0.49, 1.41, 3.29, 1.22, 0.56, 0.1, 0.67, 2.38,
0.43, 1.56, 0.07, 0.08, 1.53, -0.01, 0.12, 0.1, 0.04, 3.42, 0.23,
0, 0.34, 0.15, 0.03, 0.19, 0.17, 0.2, 0.09, 2.3, 0.07, 0.15,
0.18, 1.07, 1.21, 3.4, 0.8, -0.04, 0.02, 0.74, 1.59, 0.71, 10.64,
0.64, -0.01, 1.06, 0.81, 4.58, 0.01, 0.14, 0.59, 7.35, 0.63,
0.17, 0.38, -0.08, 1.1, 0.89, 0.94, 1.52, 0.01, 0.1, 0.38, 0.02
)), .Names = c("group", "point", "x", "y"), class = "data.frame", row.names = c(NA,
-95L))
Here's the plot my attempts to overlay a legend:
> attach(v1)
> plot(x,y, pch=16, col=group) #simple plot, automatic colors
> #first legend
> legend("topleft", legend=group, pch=16, col=group)
> # colors matched, but it's breaking out every point
> legend("topright", legend=levels(group), pch=16, col=group)
> # Corrected the number of levels in legend, but no colors
>
You can see that the first legend appears correct color-wise, but it shows an entry for every point and runs out of space. The second legend shows group as factor levels, which is what I want, but it doesn't change the colors.
I realize that I could color as a vector (e.g. col(c("black","red", etc.), but since the original plot command automatically assigned colors, I'm looking to do it "automatically" in my legend and avoid the risk of putting the wrong colors in my vector.
Thanks!
base R solution:
attach(v1)
plot(x,y, pch=16, col=group)
legend("topleft", legend=levels(group), pch=16, col=unique(group))
ggplot2 solution
ggplot(v1)+
geom_point(aes(x=x,y=y,colour=group))+
theme_bw()
Again, I would strongly suggest the use of ggplot2 over base R unless you're only exploring the data. There are plenty of questions/answers on the matter on SO.
Try creating a new column in v1 that is a number based on the value of group (as a factor). Pass this column as the col when plotting the points. Then create a vector of numbers for legend in the same way and pass that as the col for legend.
v1$cols = as.numeric(as.factor(v1$group))
legend.cols = as.numeric(as.factor(levels(v1$group)))
plot(v1$x , v1$y, pch=16, col=v1$cols)
legend("topright", legend=levels(group), pch=16, col=legend.cols)

R fast matrix formulation

I need a fast way in R to insert a matrix into another matrix in R. My current matrix shape is block diagonal shaped like this:
library(Matrix)
block1 <- new("dgCMatrix" , i = c(0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 3L, 4L,
5L, 3L, 4L, 5L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 6L, 7L, 8L,
9L, 6L, 7L, 8L, 9L, 6L, 7L, 8L, 9L) ,
p = c(0L, 3L, 6L, 9L, 12L, 15L, 18L, 22L, 26L, 30L, 34L) ,
Dim = c(10L, 10L) , Dimnames = list(NULL, NULL) ,
x = c(1.002, 0.002, 0, 0.002, 1.002, 0.002, 0, 0.002, 1.002,
1.002, 0.002, 0, 0.002, 1.002, 0.002, 0, 0.002, 1.002,
3.002, 1.924, 0.507, 0.055, 1.924, 3.002, 1.924,
0.507, 0.507, 1.924, 3.002, 1.924, 0.055, 0.507, 1.924, 3.002) ,
factors = list())
I want to insert the matrix below into the elements [4:10,1:6] of the matrix above.
block2 <-
structure(c(0.779, 0.018, 0, 0.779, 0.018, 0, 0.256, 0.169, 0, 0.256,
0.169, 0, 0.035, 0.641, 0, 0.035, 0.641, 0, 0.002, 1,
0.002, 0.002, 1, 0.002), .Dim = c(6L, 4L))
I am currenly doing so using:
block1[1:6,7:10]=block2
However this takes a lot of time if my matrix is huge. Is there any other function or clever method I can use?

Add a constant line to all plots in facet_wrap()

I have the following code:
p1 <- ggplot(df_test, aes(x=AA_Number,y=Energy_Profile,col='red')) + geom_line() + facet_wrap(~Model, ncol=3) + geom_hline(yintercept=-0.03, colour='blue') + geom_line(data=df_templates, colour="green")
print(p1)
It produces this output:
I am having trouble merging the data in green into one plot and then plotting it over the other three plots in red.
Essentially the plot in green is my constant and I want to see how my data in red varies from the constant, by overlaying the data in green on top of each of the plots in red.
Anybody have any ideas?
Data:
df_test:
structure(list(Model = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("102",
"103", "104", "105", "107", "108", "109", "118", "141", "143",
"144", "145", "14x", "161", "162", "163", "164", "165", "167",
"168", "169", "2", "21", "22", "25", "26", "27", "3", "31", "310",
"32", "36", "37", "39", "51", "510", "52", "53", "54", "57",
"61", "62", "64", "65", "66", "67", "68", "81", "84", "88", "910",
"93", "95", "97"), class = "factor"), AA_Number = 1:614, AA = structure(c(1L,
15L, 1L, 10L, 11L, 4L, 18L, 18L, 1L, 9L, 12L, 7L, 19L, 11L, 14L,
16L, 4L, 4L, 10L, 11L, 16L, 9L, 11L, 16L, 1L, 18L, 2L, 2L, 10L,
1L, 2L, 11L, 18L, 8L, 14L, 4L, 19L, 15L, 11L, 6L, 14L, 4L, 1L,
6L, 1L, 2L, 13L, 11L, 6L, 19L, 1L, 6L, 1L, 17L, 6L, 19L, 10L,
15L, 4L, 6L, 5L, 2L, 2L, 1L, 6L, 18L, 4L, 5L, 12L, 17L, 5L, 8L,
2L, 3L, 4L, 14L, 5L, 13L, 3L, 2L, 5L, 13L, 8L, 8L, 7L, 4L, 7L,
5L, 15L, 13L, 3L, 1L, 6L, 4L, 13L, 4L, 4L, 9L, 13L, 15L, 15L,
9L, 13L, 7L, 5L, 8L, 1L, 2L, 5L, 15L, 2L, 5L, 2L, 14L, 1L, 16L,
2L, 7L, 8L, 4L, 9L, 6L, 9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L,
16L, 10L, 16L, 6L, 8L, 11L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L,
19L, 18L, 3L, 1L, 13L, 7L, 12L, 9L, 6L, 5L, 6L, 9L, 14L, 11L,
16L, 10L, 3L, 19L, 11L, 9L, 14L, 1L, 7L, 19L, 7L, 3L, 7L, 5L,
2L, 9L, 2L, 10L, 11L, 7L, 5L, 7L, 16L, 14L, 7L, 6L, 3L, 7L, 7L,
14L, 3L, 7L, 4L, 10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L, 8L,
14L, 10L, 13L, 4L, 9L, 19L, 7L, 5L, 4L, 10L, 6L, 9L, 19L, 19L,
14L, 19L, 15L, 14L, 17L, 6L, 1L, 1L, 10L, 7L, 1L, 11L, 19L, 16L,
19L, 14L, 16L, 10L, 11L, 6L, 15L, 9L, 10L, 10L, 16L, 9L, 14L,
14L, 7L, 15L, 5L, 1L, 7L, 5L, 2L, 10L, 2L, 19L, 11L, 7L, 11L,
7L, 10L, 19L, 15L, 11L, 11L, 5L, 16L, 7L, 4L, 10L, 18L, 1L, 19L,
10L, 11L, 9L, 19L, 12L, 14L, 14L, 11L, 14L, 4L, 6L, 3L, 16L,
1L, 1L, 10L, 17L, 5L, 5L, 10L, 1L, 4L, 1L, 5L, 15L, 15L, 13L,
4L, 14L, 2L, 11L, 4L, 17L, 7L, 11L, 1L, 1L, 15L, 1L, 10L, 11L,
4L, 18L, 18L, 1L, 9L, 12L, 7L, 19L, 11L, 14L, 16L, 4L, 4L, 10L,
11L, 16L, 9L, 11L, 16L, 1L, 18L, 2L, 2L, 10L, 1L, 2L, 11L, 18L,
8L, 14L, 4L, 19L, 15L, 11L, 6L, 14L, 4L, 1L, 6L, 1L, 2L, 13L,
11L, 6L, 19L, 1L, 6L, 1L, 17L, 6L, 19L, 10L, 15L, 4L, 6L, 5L,
2L, 2L, 1L, 6L, 18L, 4L, 5L, 12L, 17L, 5L, 8L, 2L, 3L, 4L, 14L,
5L, 13L, 3L, 2L, 5L, 13L, 8L, 8L, 7L, 4L, 7L, 5L, 15L, 13L, 3L,
1L, 6L, 4L, 13L, 4L, 4L, 9L, 13L, 15L, 15L, 9L, 13L, 7L, 5L,
8L, 1L, 2L, 5L, 15L, 2L, 5L, 2L, 14L, 1L, 16L, 2L, 7L, 8L, 4L,
9L, 6L, 9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L, 16L, 10L, 16L,
6L, 8L, 11L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L, 19L, 18L, 3L,
1L, 13L, 7L, 12L, 9L, 6L, 5L, 6L, 9L, 14L, 11L, 16L, 10L, 3L,
19L, 11L, 9L, 14L, 1L, 7L, 19L, 7L, 3L, 7L, 5L, 2L, 9L, 2L, 10L,
11L, 7L, 5L, 7L, 16L, 14L, 7L, 6L, 3L, 7L, 7L, 14L, 3L, 7L, 4L,
10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L, 8L, 14L, 10L, 13L, 4L,
9L, 19L, 7L, 5L, 4L, 10L, 6L, 9L, 19L, 19L, 14L, 19L, 15L, 14L,
17L, 6L, 1L, 1L, 10L, 7L, 1L, 11L, 19L, 16L, 19L, 14L, 16L, 10L,
11L, 6L, 15L, 9L, 10L, 10L, 16L, 9L, 14L, 14L, 7L, 15L, 5L, 1L,
7L, 5L, 2L, 10L, 2L, 19L, 11L, 7L, 11L, 7L, 10L, 19L, 15L, 11L,
11L, 5L, 16L, 7L, 4L, 10L, 18L, 1L, 19L, 10L, 11L, 9L, 19L, 12L,
14L, 14L, 11L, 14L, 4L, 6L, 3L, 16L, 1L, 1L, 10L, 17L, 5L, 5L,
10L, 1L, 4L, 1L, 5L, 15L, 15L, 13L, 4L, 14L, 2L, 11L, 4L, 17L,
7L, 11L, 1L), .Label = c("ALA", "ARG", "ASN", "ASP", "GLN", "GLU",
"GLY", "HIS", "ILE", "LEU", "LYS", "MET", "PHE", "PRO", "SER",
"THR", "TRP", "TYR", "VAL"), class = "factor"), Energy_Profile = c(-0.017,
-0.018, -0.02, -0.021, -0.022, -0.024, -0.026, -0.027, -0.028,
-0.028, -0.028, -0.026, -0.025, -0.024, -0.022, -0.021, -0.02,
-0.02, -0.021, -0.022, -0.023, -0.024, -0.024, -0.026, -0.026,
-0.025, -0.024, -0.023, -0.023, -0.022, -0.021, -0.02, -0.02,
-0.02, -0.019, -0.018, -0.019, -0.019, -0.02, -0.02, -0.022,
-0.023, -0.024, -0.025, -0.026, -0.027, -0.027, -0.026, -0.026,
-0.027, -0.027, -0.027, -0.027, -0.026, -0.026, -0.025, -0.024,
-0.022, -0.021, -0.021, -0.02, -0.02, -0.021, -0.023, -0.024,
-0.025, -0.026, -0.027, -0.028, -0.028, -0.028, -0.027, -0.027,
-0.027, -0.027, -0.027, -0.026, -0.026, -0.026, -0.025, -0.025,
-0.024, -0.024, -0.024, -0.024, -0.024, -0.025, -0.026, -0.027,
-0.028, -0.028, -0.028, -0.028, -0.028, -0.027, -0.026, -0.025,
-0.026, -0.026, -0.025, -0.026, -0.027, -0.027, -0.027, -0.025,
-0.024, -0.023, -0.022, -0.02, -0.019, -0.018, -0.019, -0.019,
-0.02, -0.021, -0.022, -0.023, -0.024, -0.025, -0.027, -0.029,
-0.031, -0.034, -0.035, -0.035, -0.035, -0.034, -0.032, -0.03,
-0.026, -0.024, -0.023, -0.022, -0.022, -0.023, -0.024, -0.027,
-0.029, -0.032, -0.035, -0.036, -0.037, -0.037, -0.037, -0.036,
-0.035, -0.033, -0.032, -0.032, -0.031, -0.031, -0.032, -0.033,
-0.034, -0.036, -0.038, -0.04, -0.041, -0.042, -0.044, -0.045,
-0.044, -0.043, -0.041, -0.04, -0.037, -0.035, -0.032, -0.031,
-0.029, -0.029, -0.029, -0.029, -0.03, -0.031, -0.031, -0.031,
-0.031, -0.029, -0.027, -0.025, -0.023, -0.021, -0.019, -0.018,
-0.017, -0.018, -0.019, -0.023, -0.026, -0.031, -0.036, -0.04,
-0.043, -0.045, -0.044, -0.043, -0.039, -0.037, -0.033, -0.03,
-0.028, -0.027, -0.027, -0.026, -0.025, -0.025, -0.025, -0.024,
-0.023, -0.023, -0.024, -0.025, -0.026, -0.028, -0.029, -0.029,
-0.029, -0.029, -0.029, -0.029, -0.028, -0.029, -0.031, -0.033,
-0.034, -0.036, -0.037, -0.039, -0.039, -0.039, -0.038, -0.037,
-0.036, -0.035, -0.033, -0.033, -0.032, -0.031, -0.03, -0.029,
-0.028, -0.027, -0.025, -0.023, -0.022, -0.021, -0.02, -0.019,
-0.019, -0.018, -0.019, -0.018, -0.018, -0.017, -0.017, -0.016,
-0.015, -0.014, -0.013, -0.012, -0.011, -0.011, -0.013, -0.014,
-0.017, -0.02, -0.024, -0.027, -0.03, -0.031, -0.032, -0.03,
-0.029, -0.027, -0.025, -0.023, -0.021, -0.021, -0.022, -0.023,
-0.025, -0.026, -0.028, -0.031, -0.033, -0.035, -0.035, -0.035,
-0.036, -0.036, -0.034, -0.033, -0.031, -0.028, -0.025, -0.023,
-0.021, -0.019, -0.017, -0.015, -0.014, -0.013, -0.012, -0.011,
-0.011, -0.011, -0.012, -0.013, -0.016, -0.018, -0.02, -0.022,
-0.024, -0.026, -0.027, -0.027, -0.026, -0.025, -0.024, -0.023,
-0.022, -0.021, -0.02, -0.021, -0.022, -0.023, -0.024, -0.024,
-0.024, -0.025, -0.025, -0.023, -0.022, -0.021, -0.021, -0.02,
-0.018, -0.018, -0.018, -0.017, -0.016, -0.016, -0.016, -0.016,
-0.016, -0.017, -0.019, -0.02, -0.021, -0.023, -0.024, -0.026,
-0.027, -0.027, -0.027, -0.028, -0.028, -0.028, -0.028, -0.027,
-0.026, -0.025, -0.023, -0.022, -0.02, -0.02, -0.02, -0.02, -0.021,
-0.022, -0.023, -0.025, -0.025, -0.026, -0.026, -0.026, -0.026,
-0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025,
-0.025, -0.025, -0.024, -0.024, -0.024, -0.024, -0.025, -0.025,
-0.026, -0.027, -0.028, -0.028, -0.028, -0.028, -0.027, -0.027,
-0.025, -0.025, -0.026, -0.026, -0.025, -0.026, -0.027, -0.027,
-0.027, -0.025, -0.024, -0.023, -0.022, -0.02, -0.019, -0.018,
-0.019, -0.019, -0.02, -0.021, -0.022, -0.023, -0.024, -0.025,
-0.027, -0.029, -0.031, -0.034, -0.035, -0.036, -0.035, -0.034,
-0.032, -0.03, -0.027, -0.025, -0.024, -0.023, -0.023, -0.024,
-0.025, -0.027, -0.03, -0.032, -0.035, -0.036, -0.037, -0.037,
-0.037, -0.036, -0.035, -0.033, -0.032, -0.032, -0.031, -0.031,
-0.032, -0.033, -0.034, -0.036, -0.038, -0.04, -0.041, -0.042,
-0.044, -0.044, -0.044, -0.043, -0.041, -0.039, -0.037, -0.034,
-0.031, -0.03, -0.029, -0.028, -0.029, -0.029, -0.03, -0.031,
-0.031, -0.032, -0.031, -0.029, -0.027, -0.025, -0.023, -0.021,
-0.019, -0.018, -0.017, -0.018, -0.019, -0.023, -0.026, -0.031,
-0.036, -0.04, -0.043, -0.045, -0.044, -0.043, -0.039, -0.037,
-0.033, -0.03, -0.028, -0.027, -0.026, -0.026, -0.025, -0.025,
-0.024, -0.023, -0.023, -0.024, -0.026, -0.027, -0.029, -0.03,
-0.031, -0.031, -0.031, -0.03, -0.03, -0.029, -0.028, -0.029,
-0.029, -0.031, -0.033, -0.035, -0.036, -0.039, -0.039, -0.04,
-0.039, -0.039, -0.038, -0.037, -0.037, -0.036, -0.036, -0.035,
-0.034, -0.032, -0.031, -0.028, -0.026, -0.024, -0.023, -0.022,
-0.022, -0.021, -0.022, -0.022, -0.022, -0.022, -0.022, -0.021,
-0.021, -0.02, -0.019, -0.018, -0.017, -0.015, -0.014, -0.014,
-0.015, -0.017, -0.02, -0.023, -0.026, -0.029, -0.032, -0.033,
-0.033, -0.031, -0.03, -0.029, -0.027, -0.025, -0.023, -0.023,
-0.024, -0.025, -0.027, -0.029, -0.032, -0.034, -0.037, -0.039,
-0.04, -0.041, -0.041, -0.04, -0.039, -0.036, -0.033, -0.03,
-0.026, -0.023, -0.021, -0.019, -0.017, -0.016, -0.015, -0.015,
-0.014, -0.014, -0.013, -0.013)), .Names = c("Model", "AA_Number",
"AA", "Energy_Profile"), row.names = c(NA, 614L), class = "data.frame")
df_templates:
structure(list(Model = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2kqx_renumberedA",
"2kqx_renumberedB", "3lz8_renumbered"), class = "factor"), AA_Number = c(3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L,
44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L,
57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L,
70L, 71L, 72L, 73L, 310L, 311L, 312L, 313L, 314L, 315L, 316L,
317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L,
328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L,
339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L,
350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L,
361L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 369L, 370L, 371L,
372L, 373L, 374L, 375L, 376L, 377L, 378L, 379L, 380L, 115L, 116L,
117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L,
150L, 148L, 150L, 151L, 153L, 154L, 155L, 156L, 157L, 158L, 159L,
160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L,
171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 178L, 180L,
181L, 182L, 183L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L,
195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L,
206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L,
217L, 218L, 219L, 220L, 221L, 219L, 221L, 222L, 223L, 224L, 225L,
226L, 227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L,
237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L,
248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L,
259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L,
270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 278L,
280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L,
291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L,
302L, 303L, 301L, 303L, 304L, 422L, 423L, 424L, 425L, 426L, 427L,
428L, 429L, 430L, 431L, 432L, 433L, 434L, 435L, 436L, 437L, 438L,
439L, 440L, 441L, 442L, 443L, 444L, 445L, 446L, 447L, 448L, 449L,
450L, 451L, 452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L, 460L,
151L, 461L, 462L, 463L, 464L, 465L, 466L, 467L, 158L, 467L, 468L,
469L, 470L, 471L, 472L, 473L, 474L, 475L, 476L, 477L, 478L, 479L,
480L, 481L, 482L, 483L, 484L, 485L, 176L, 485L, 486L, 487L, 488L,
489L, 490L, 491L, 492L, 493L, 495L, 496L, 497L, 498L, 499L, 500L,
501L, 502L, 503L, 504L, 505L, 506L, 507L, 508L, 509L, 510L, 511L,
512L, 513L, 204L, 513L, 514L, 515L, 516L, 517L, 518L, 519L, 520L,
521L, 522L, 523L, 524L, 525L, 526L, 527L, 528L, 529L, 530L, 531L,
532L, 533L, 534L, 535L, 536L, 537L, 538L, 539L, 230L, 539L, 540L,
541L, 542L, 543L, 544L, 545L, 546L, 547L, 548L, 549L, 550L, 551L,
552L, 553L, 554L, 555L, 556L, 557L, 558L, 559L, 560L, 561L, 562L,
563L, 564L, 565L, 566L, 567L, 568L, 571L, 572L, 573L, 574L, 575L,
576L, 577L, 578L, 579L, 580L, 581L, 582L, 583L, 584L, 585L, 586L,
587L, 588L, 589L, 590L, 591L, 592L, 593L, 594L, 595L, 596L, 597L,
598L, 599L, 600L, 601L, 602L, 603L, 604L, 605L, 606L, 607L, 608L,
609L, 610L, 611L), AA = structure(c(6L, 10L, 11L, 4L, 18L, 18L,
1L, 9L, 12L, 7L, 19L, 11L, 14L, 16L, 4L, 4L, 10L, 11L, 16L, 9L,
11L, 16L, 1L, 18L, 2L, 2L, 10L, 1L, 2L, 11L, 18L, 8L, 14L, 4L,
19L, 15L, 11L, 6L, 14L, 4L, 1L, 6L, 1L, 2L, 13L, 11L, 6L, 19L,
1L, 6L, 1L, 17L, 6L, 19L, 10L, 15L, 4L, 6L, 5L, 2L, 2L, 1L, 6L,
18L, 4L, 5L, 12L, 17L, 5L, 8L, 2L, 6L, 10L, 11L, 4L, 18L, 18L,
1L, 9L, 12L, 7L, 19L, 11L, 14L, 16L, 4L, 4L, 10L, 11L, 16L, 9L,
11L, 16L, 1L, 18L, 2L, 2L, 10L, 1L, 2L, 11L, 18L, 8L, 14L, 4L,
19L, 15L, 11L, 6L, 14L, 4L, 1L, 6L, 1L, 2L, 13L, 11L, 6L, 19L,
1L, 6L, 1L, 17L, 6L, 19L, 10L, 15L, 4L, 6L, 5L, 2L, 2L, 1L, 6L,
18L, 4L, 5L, 12L, 17L, 5L, 8L, 2L, 1L, 1L, 2L, 7L, 8L, 4L, 10L,
6L, 9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L, 16L, 10L, 1L, 6L,
5L, 16L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L, 19L, 18L, 3L, 19L,
13L, 13L, 13L, 7L, 9L, 6L, 15L, 6L, 16L, 14L, 11L, 16L, 10L,
3L, 19L, 11L, 9L, 14L, 1L, 7L, 19L, 19L, 4L, 7L, 5L, 2L, 9L,
2L, 10L, 11L, 7L, 5L, 5L, 7L, 16L, 14L, 7L, 7L, 14L, 3L, 7L,
4L, 10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L, 8L, 14L, 10L, 13L,
4L, 9L, 19L, 7L, 8L, 3L, 10L, 6L, 9L, 19L, 10L, 14L, 10L, 1L,
14L, 17L, 17L, 17L, 6L, 1L, 1L, 10L, 7L, 1L, 11L, 19L, 16L, 19L,
14L, 16L, 10L, 11L, 6L, 15L, 9L, 10L, 10L, 16L, 19L, 14L, 14L,
7L, 15L, 5L, 1L, 7L, 5L, 2L, 10L, 2L, 9L, 11L, 7L, 11L, 7L, 10L,
19L, 15L, 11L, 16L, 8L, 16L, 7L, 4L, 10L, 13L, 1L, 19L, 9L, 11L,
9L, 19L, 12L, 14L, 16L, 11L, 14L, 14L, 4L, 6L, 11L, 1L, 2L, 6L,
10L, 17L, 5L, 5L, 10L, 1L, 1L, 1L, 6L, 1L, 15L, 13L, 4L, 14L,
2L, 11L, 16L, 16L, 16L, 17L, 1L, 1L, 2L, 7L, 8L, 4L, 10L, 6L,
9L, 6L, 19L, 1L, 19L, 13L, 10L, 6L, 6L, 16L, 10L, 1L, 6L, 5L,
16L, 2L, 16L, 9L, 15L, 18L, 3L, 10L, 14L, 19L, 18L, 3L, 19L,
13L, 7L, 12L, 9L, 9L, 6L, 15L, 6L, 16L, 14L, 11L, 16L, 16L, 16L,
10L, 3L, 19L, 11L, 9L, 14L, 1L, 7L, 19L, 19L, 4L, 7L, 5L, 2L,
9L, 2L, 10L, 11L, 11L, 11L, 7L, 5L, 7L, 16L, 14L, 7L, 6L, 3L,
7L, 14L, 3L, 7L, 4L, 10L, 17L, 10L, 19L, 9L, 8L, 9L, 1L, 14L,
8L, 14L, 10L, 13L, 4L, 4L, 4L, 9L, 19L, 7L, 8L, 3L, 10L, 6L,
9L, 19L, 10L, 14L, 10L, 1L, 14L, 17L, 6L, 1L, 1L, 10L, 7L, 1L,
11L, 19L, 16L, 19L, 14L, 14L, 14L, 16L, 10L, 11L, 6L, 15L, 9L,
10L, 10L, 16L, 19L, 14L, 14L, 7L, 15L, 5L, 1L, 7L, 5L, 2L, 10L,
2L, 9L, 11L, 7L, 11L, 7L, 10L, 19L, 15L, 8L, 16L, 7L, 4L, 10L,
13L, 1L, 19L, 9L, 11L, 9L, 19L, 12L, 14L, 16L, 11L, 14L, 4L,
6L, 11L, 1L, 2L, 6L, 10L, 17L, 5L, 5L, 10L, 1L, 1L, 1L, 6L, 1L,
15L, 13L, 4L, 14L, 2L, 11L, 16L, 17L), .Label = c("ALA", "ARG",
"ASN", "ASP", "GLN", "GLU", "GLY", "HIS", "ILE", "LEU", "LYS",
"MET", "PHE", "PRO", "SER", "THR", "TRP", "TYR", "VAL"), class = "factor"),
Energy_Profile = c(-0.018, -0.019, -0.019, -0.02, -0.022,
-0.023, -0.024, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025,
-0.026, -0.025, -0.026, -0.028, -0.029, -0.029, -0.027, -0.026,
-0.026, -0.026, -0.025, -0.023, -0.023, -0.024, -0.025, -0.025,
-0.024, -0.025, -0.025, -0.026, -0.025, -0.024, -0.024, -0.024,
-0.023, -0.024, -0.025, -0.025, -0.027, -0.028, -0.029, -0.029,
-0.029, -0.028, -0.027, -0.025, -0.023, -0.022, -0.022, -0.022,
-0.023, -0.024, -0.025, -0.027, -0.028, -0.029, -0.03, -0.03,
-0.032, -0.032, -0.033, -0.033, -0.033, -0.033, -0.033, -0.032,
-0.031, -0.029, -0.018, -0.019, -0.019, -0.02, -0.022, -0.023,
-0.024, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.026,
-0.025, -0.026, -0.028, -0.029, -0.029, -0.027, -0.026, -0.026,
-0.026, -0.025, -0.023, -0.023, -0.024, -0.025, -0.025, -0.024,
-0.025, -0.025, -0.026, -0.025, -0.024, -0.024, -0.024, -0.023,
-0.024, -0.025, -0.025, -0.027, -0.028, -0.029, -0.029, -0.029,
-0.028, -0.027, -0.025, -0.023, -0.022, -0.022, -0.022, -0.023,
-0.024, -0.025, -0.027, -0.028, -0.029, -0.03, -0.03, -0.032,
-0.032, -0.033, -0.033, -0.033, -0.033, -0.033, -0.032, -0.031,
-0.029, -0.015, -0.019, -0.023, -0.026, -0.029, -0.032, -0.037,
-0.04, -0.044, -0.046, -0.047, -0.046, -0.046, -0.044, -0.042,
-0.039, -0.038, -0.037, -0.037, -0.038, -0.038, -0.039, -0.041,
-0.042, -0.043, -0.044, -0.044, -0.045, -0.045, -0.043, -0.035,
-0.024, -0.01, 0.0021, 0.014, 0.027, 0.037, 0.035, 0.026,
0.015, 0.0039, -0.008, -0.021, -0.032, -0.039, -0.042, -0.045,
-0.048, -0.049, -0.05, -0.049, -0.048, -0.046, -0.043, -0.04,
-0.039, -0.037, -0.036, -0.037, -0.037, -0.037, -0.035, -0.032,
-0.028, -0.024, -0.019, -0.012, -0.0097, -0.011, -0.013,
-0.014, -0.017, -0.023, -0.03, -0.035, -0.04, -0.044, -0.049,
-0.053, -0.055, -0.055, -0.053, -0.05, -0.048, -0.045, -0.043,
-0.041, -0.04, -0.041, -0.041, -0.041, -0.041, -0.042, -0.042,
-0.043, -0.044, -0.046, -0.047, -0.048, -0.049, -0.047, -0.042,
-0.038, -0.032, -0.027, -0.021, -0.015, -0.012, -0.011, -0.012,
-0.013, -0.015, -0.018, -0.024, -0.029, -0.034, -0.037, -0.04,
-0.043, -0.044, -0.045, -0.044, -0.045, -0.045, -0.045, -0.045,
-0.045, -0.043, -0.041, -0.039, -0.036, -0.034, -0.033, -0.033,
-0.034, -0.035, -0.036, -0.038, -0.039, -0.039, -0.04, -0.039,
-0.039, -0.039, -0.039, -0.04, -0.04, -0.039, -0.04, -0.041,
-0.042, -0.043, -0.044, -0.046, -0.048, -0.049, -0.05, -0.05,
-0.049, -0.046, -0.043, -0.037, -0.031, -0.025, -0.021, -0.017,
-0.015, -0.016, -0.021, -0.028, -0.034, -0.039, -0.044, -0.049,
-0.051, -0.049, -0.047, -0.045, -0.043, -0.04, -0.038, -0.036,
-0.035, -0.035, -0.033, -0.03, -0.026, -0.022, -0.017, -0.012,
-0.0058, -0.002, -0.0018, -0.0043, -0.0073, -0.012, -0.017,
-0.023, -0.029, -0.034, -0.038, -0.041, -0.044, -0.046, -0.047,
-0.046, -0.045, -0.043, -0.041, -0.038, -0.036, -0.034, -0.034,
-0.035, -0.035, -0.037, -0.039, -0.041, -0.043, -0.045, -0.046,
-0.047, -0.048, -0.05, -0.052, -0.054, -0.054, -0.051, -0.044,
-0.037, -0.027, -0.017, -0.0058, 0.0041, 0.0065, 0.0052,
0.0053, 0.0048, 0.0059, 0.0058, 0.0057, 0.0074, 0.007, 0.0014,
-0.0049, -0.015, -0.023, -0.031, -0.037, -0.04, -0.039, -0.039,
-0.037, -0.036, -0.034, -0.029, -0.023, -0.017, -0.009, -0.0017,
0.007, 0.011, 0.01, 0.0045, -0.0012, -0.0089, -0.017, -0.025,
-0.031, -0.033, -0.034, -0.035, -0.036, -0.038, -0.04, -0.042,
-0.046, -0.049, -0.052, -0.052, -0.051, -0.049, -0.046, -0.042,
-0.035, -0.027, -0.019, -0.013, -0.0065, -6.1e-05, 0.0045,
0.003, -0.0013, -0.0071, -0.014, -0.021, -0.029, -0.036,
-0.041, -0.044, -0.046, -0.046, -0.045, -0.044, -0.043, -0.041,
-0.039, -0.038, -0.038, -0.038, -0.039, -0.039, -0.038, -0.034,
-0.03, -0.025, -0.02, -0.013, -0.0082, -0.008, -0.011, -0.016,
-0.021, -0.028, -0.034, -0.04, -0.042, -0.043, -0.041, -0.04,
-0.038, -0.035, -0.033, -0.032, -0.032, -0.033, -0.034, -0.035,
-0.037, -0.038, -0.038, -0.039, -0.038, -0.038, -0.037, -0.037,
-0.036, -0.037, -0.036, -0.037, -0.039, -0.041, -0.043, -0.045,
-0.046, -0.048, -0.048, -0.047, -0.045, -0.043, -0.04, -0.038,
-0.037, -0.036, -0.037, -0.039, -0.041, -0.043, -0.046, -0.047,
-0.048, -0.048, -0.048, -0.046, -0.042, -0.04, -0.038, -0.036,
-0.033, -0.032, -0.031, -0.032, -0.032, -0.033, -0.034, -0.035,
-0.036, -0.037, -0.038, -0.039)), .Names = c("Model", "AA_Number",
"AA", "Energy_Profile"), class = "data.frame", row.names = c(NA,
-532L))
In my df_test data I provided here, I could only put one plot as I reach the character limit.
Your data frame df_templates is also faceted because it has the same column Model as given in facet_wrap(). If you rename this column, for example, to Model2
colnames(df_templates)<-c("AA_Number","AA","Energy_Profile","Model2")
Then this data frame is not faceted.
ggplot(df_test, aes(x=AA_Number,y=Energy_Profile,col='red')) + geom_line() +
geom_hline(yintercept=-0.03, colour='blue') +
geom_line(data=df_templates,colour="green")+
facet_wrap(~Model,ncol=3)

Resources