Remove margin inside plot pf ggplot2 - r

this is my script and the associated plot:
library(ggplot2)
library(reshape)
df <- structure(list(ID = structure(1:19, .Label = c("2818/22/0834",
"2818/22/0851", "2818/22/0853", "2818/22/0886", "B0F", "B12T",
"B1T", "B21T", "B22F", "B26T", "B33F", "B4F", "P1", "P21", "P24",
"P25", "P27", "P28", "P29"), class = "factor"), K = c(0.089,
0.094, 0.096, 0.274, 0.09, 0.312, 0.33, 0.178, 0.05, 0.154, 0.083,
0.098, 0.035, 0.084, 0.053, 0.061, 0.043, 0.094, 0.101), Na = c(2.606,
3.822, 4.977, 2.522, 15.835, 83.108, 52.041, 41.448, 11.849,
40.531, 5.854, 10.151, 3.52, 8.445, 5.273, 7.246, 6.177, 14.813,
15.569), Cl = c(3.546, 6.181, 8.422, 3.733, 14.685, 96.911, 65.518,
79.01, 10.349, 53.361, 6.12, 10.832, 2.313, 10.312, 5.641, 8.708,
6.138, 12.302, 20.078), Mg = c(1.487, 1.773, 1.992, 1.143, 2.991,
1.678, 2.23, 3.288, 1.148, 2.428, 3.428, 2.729, 0.777, 2.554,
2.374, 4.075, 1.993, 1.881, 3.034), Ca = c(5.529, 6.205, 6.59,
4.099, 10.631, 4.564, 6.652, 13.374, 4.332, 10.542, 11.194, 10.053,
2.969, 7.73, 8.163, 11.539, 6.166, 5.968, 9.299), SO4 = c(0.663,
0.831, 0.607, 0.882, 9.013, 0.896, 0.652, 0.021, 1.446, 0.012,
8.832, 6.665, 1.003, 2.575, 3.685, 7.121, 3.64, 5.648, 2.397),
HCO3 = c(7.522, 5.498, 6.15, 5.242, 8.582, 4.067, 5.65, 9.364,
5.435, 8.068, 9.054, 8.326, 4.805, 7.235, 7.488, 9.234, 6.352,
6.98, 8.34)), .Names = c("ID", "K", "Na", "Cl", "Mg", "Ca",
"SO4", "HCO3"), class = "data.frame", row.names = c(NA, -19L))
df_melted<-melt(df, na.rm=T)
ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
geom_point(size=2) +
geom_line() +
theme(legend.position="none") +
scale_y_log10(breaks=seq(0, 100, 10))
Is there a way to remove the spaces at the beginning and at the end of the plot? I tried with xlim but the problem is that the x variable is not a numerical variable, so, something like xlim(c("K", "HCO3")) doesn't work.

This is a discrete scale, but you can still used the expand argument as follows. Whether the output looks acceptable or not is another matter. Play with the c(0,0) values until you find something that suits. Using 0.1 for the second value gives a slightly better plot, in my view...
ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
geom_point(size=2) +
geom_line() +
theme(legend.position="none") +
scale_y_log10(breaks=seq(0, 100, 10)) +
scale_x_discrete(expand = c(0,0))

Related

How to adjust the limit of primary and secondary y-axis?

I want to adjust y-axis limit both for primary and secondary axis by using the example of case: How to limit primary y-axis and secondary y-axis? and ggplot with 2 y axes on each side and different scales
I want to adjust primary y-axis from 0-50 and secondary y-axis from 0-500, then don't want to show any plot with higher value than the limit (even though there are some data with values higher than the limit).
I didn't get any error with my code but the limit that I have set is not been successfully applied to the graph.
Here is my example of data:
df2 <- structure(list(startdate = structure(c(17903, 17910, 17917, 17924,
17931, 17938, 17945, 17952, 17959, 17966, 17982, 17987, 18001,
18003, 18015, 18022, 18029, 18031, 18036, 18043, 18050, 18057,
18064, 18072, 18079, 18085, 18099, 18106, 18113, 18120, 18127,
18134, 18141, 18148, 18155, 18162, 18169, 18183, 18197, 18204,
18211, 18218, 18225, 18227, 18232, 18234, 18239, 18246, 18253,
18267, 18274, 18281, 18288, 18295, 18302, 18309, 18316, 18323,
18330, 18337, 18344, 18351, 18358, 18365, 18373, 18379, 18386,
18393, 18400, 18407, 18414, 18421, 18428, 18430, 18435, 18442,
18449, 18456, 18463, 18472, 18477, 18484, 18491, 18498, 18505,
18514, 18519, 18526, 18533, 18540, 18547, 18554, 18561, 18568,
18575, 18583, 18589, 18596, 18603, 18610, 18617, 18624), class = "Date"),
Al = c(24.744, 19.272, 15.245, 21.497, 26.086, 5.867, 23.722,
30.269, 25.666, 17.106, 53.07, NA, 226.995, 70.341, 108.865,
18.15, 445.203, 393.528, 11.151, 52.329, 37.737, 16.68, 124.039,
22.667, 19.125, 82.391, 87.85, 19.041, 77.098, 34.27, 10.912,
116.28, 42.9, 9.282, 35.504, NA, 133.95, 94.311, 124.97,
63.374, 99.062, 54.366, 38.925, 66.56, 19.525, 221.973, 140.54,
68.699, 117.965, 456, 13, 44.5, 46.6, 69.4, NA, 12.3, 6.81,
NA, 20.6, 19.6, NA, 24.2, 71.6, 566, 219, 158, 58.2, 217,
351, 13.6, 38.3, 91.5, 90.2, 23.8, 23.4, 21.4, 42.9, 13.8,
NA, 35.8, 24, 9.11, 32.6, 24.6, 286, NA, 28.9, 10, NA, 331,
101, 6.58, 83.9, 2230, 1100, NA, NA, 638, 622, 143, 96, 28.3
), Fe = c(9.627, 12.429, 10.115, 9.498, 14.555, 4.39, 12.201,
12.888, 12.318, 9.889, 19.607, 11.202, 51.294, 21.877, 43.531,
9.539, 131.812, 123.998, 7.991, 21.365, 18.732, 8.378, 42.805,
5.886, 10.994, 29.268, NA, 7.832, 15.377, 12.558, 4.829,
42.002, 16.464, 5.545, 17.778, NA, 67.634, 37.384, 49.764,
28.589, 37.174, 21.271, 16.639, 29.878, 11.689, 90.459, 36.085,
15.883, 34.31, 210, 7.55, 21.8, 23.4, 32.2, 8.5, 5.76, 4.83,
1.85, 10.9, 10.5, 2.16, 12.4, 34.1, 212, 106, 65.1, 26.9,
93.1, 163, 6.41, 15.4, 34.7, 36, 10.1, 14.7, 11, 23, 5.36,
1.72, 23.3, 20.2, 6.64, 20.1, 14.3, 129, NA, 13.9, 6.6, NA,
193, 42.1, 4.29, 37.7, 1260, 585, NA, NA, 288, 289, 64.6,
43, 14.1), Mn = c(0.184, 0.377, 0.334, 0.163, 0.416, 0.101,
0.351, 0.359, 0.302, 0.406, 0.393, 0.277, 2.624, 0.656, 0.822,
0.205, 2.401, 2.403, 0.161, 0.415, NA, 0.155, 1.416, 0.134,
0.212, NA, NA, 0.337, 0.898, 0.217, NA, 1.027, 0.264, NA,
0.284, NA, 1.176, 0.599, 0.808, 0.462, 0.826, 0.487, 0.293,
0.518, 0.242, 1.848, 1.083, 0.483, 0.732, 4.22, 0.227, 0.564,
0.446, 0.624, 0.178, 0.198, 0.25, 0.054, 0.245, 0.296, 0.071,
0.304, 0.739, 4.4, 1.62, 0.987, 0.405, 1.45, 3.04, 0.121,
0.447, 0.756, 0.559, 0.201, 0.3, 0.136, 0.431, 0.885, NA,
0.456, 0.366, 0.217, 0.257, 0.208, 3.59, NA, 0.208, 0.091,
NA, 4.91, 0.685, 0.076, 0.7, 22.3, 11.1, NA, NA, 4.21, 5.26,
1.08, 0.722, 0.269), Ti = c(1.032, 0.763, 0.795, 0.861, 1.263,
0.426, 1.168, 1.284, 1.257, 0.706, 1.566, 0.965, 3.978, 1.939,
4.109, 0.787, 11.025, 12.884, 0.691, 1.58, 1.541, 0.788,
4.588, 0.45, 0.873, 2.115, NA, 1.636, 1.195, 0.971, 0.37,
3.132, 1.351, 0.328, 1.222, NA, 4.251, 2.502, 3.157, 2.044,
2.627, 1.698, 1.34, 1.879, 0.77, 4.539, 2.46, 1.17, 2.2,
8.52, 0.492, 1.41, 1.65, 2.1, 0.652, 0.368, 0.277, NA, 0.762,
0.712, NA, 0.787, 2.2, 11.6, 5.29, 4.65, 2.16, 5.38, 13.7,
0.555, 1.41, 2.78, 3.15, 0.88, 0.955, 0.853, 1.65, 0.379,
NA, 1.05, 1.06, 0.574, 1.45, 1.02, 7.83, NA, 1.06, 0.501,
NA, 8.52, 2.96, 0.339, 2.68, 47.2, 23.2, NA, NA, 13.2, 12.9,
4.11, 2.14, 1.1)), row.names = c(NA, -102L), class = c("tbl_df",
"tbl", "data.frame"))
And here is the code:
library(ggplot2)
library(dplyr)
library(tidyr)
library(lubridate)
library(scales)
label_y1 = expression(bold(Mn,Ti~(ng/m^{3})))
label_y2 = expression(bold(Al,Fe~(ng/m^{3})))
#Determine certain date for shading
shade <- df2 %>% transmute(year = year(startdate)) %>% unique() %>%
mutate( from = as.Date(paste0(year, "-02-14")), to = as.Date(paste0(year, "-05-07")))
# Function factory for secondary axis transforms
train_sec <- function(primary, secondary) {
from <- range(secondary)
to <- range(primary)
# Forward transform for the data
forward <- function(x) {
rescale(x, from = from, to = to)
}
# Reverse transform for the secondary axis
reverse <- function(x) {
rescale(x, from = to, to = from)
}
list(fwd = forward, rev = reverse)
#Set the limit of both y-axis
sec <- train_sec(c(0, 50), c(0, 500))
#Plotting data
ggplot(df2) +
geom_line( aes(x=startdate, y=Mn, color='Mn')) +
geom_line( aes(x=startdate, y=Ti, color='Ti')) +
geom_line( aes(x=startdate, y= sec$fwd(Al), color = 'Al')) +
geom_line( aes(x=startdate, y= sec$fwd(Fe), color = 'Fe')) +
geom_rect(data = shade, aes(xmin = from, xmax = to, ymin = -Inf, ymax = Inf), fill = 'red',alpha=0.1) +
scale_y_continuous(name = label_y1, sec.axis = sec_axis(~sec$rev(.), name = label_y2))+ ggtitle ("a)")+
theme_bw()+ theme(legend.position = c(0.1, 0.9),legend.direction="horizontal", axis.text.x = element_text(face="bold", size=10) ,axis.text.y = element_text(face="bold", size=10), axis.title = element_text(size = 10), plot.title = element_text(size=10, face="bold", hjust=0.05,vjust = - 12), legend.spacing.y = unit(0, "mm"), axis.text = element_text(colour = 1),legend.background = element_blank(),legend.box.background = element_blank(), legend.key = element_blank(), legend.justification = "left")+labs(color = NULL, fill = NULL, x=NULL)+guides(colour = guide_legend(override.aes = list(size=1)))
I'm not really sure why the limitation to y-axis is not successful. If anybody know the reason and how to fix this, please let me know. I really appreciate it.
Thank you so much. Best regards.
I added limits directly into scale_y_continous and I think this works
ggplot(df2) +
geom_line( aes(x=startdate, y=Mn, color='Mn')) +
geom_line( aes(x=startdate, y=Ti, color='Ti')) +
geom_line( aes(x=startdate, y= sec$fwd(Al), color = 'Al')) +
geom_line( aes(x=startdate, y= sec$fwd(Fe), color = 'Fe')) +
geom_rect(data = shade, aes(xmin = from, xmax = to, ymin = -Inf, ymax = Inf), fill = 'red',alpha=0.1) +
scale_y_continuous(
limits = c(0, 50),
name = label_y1, sec.axis = sec_axis(~sec$rev(.), name = label_y2)
)+
ggtitle ("a)") +
theme_bw() +
theme(
legend.position = c(0.1, 0.9),
legend.direction="horizontal",
axis.text.x = element_text(face="bold", size=10),
axis.text.y = element_text(face="bold", size=10),
axis.title = element_text(size = 10),
plot.title = element_text(size=10, face="bold", hjust=0.05,vjust = - 12),
legend.spacing.y = unit(0, "mm"),
axis.text = element_text(colour = 1),
legend.background = element_blank(),
legend.box.background = element_blank(),
legend.key = element_blank(),
legend.justification = "left"
) +
labs(color = NULL, fill = NULL, x=NULL) +
guides(colour = guide_legend(override.aes = list(size=1)))

Pie Chart using R

I want to create a piechart (showing Forest.Area.ha. by GaPa_NaPa) based on following attribute table as below:-
The dataframe for the data is as shown:-
structure(list(GaPa_NaPa = c("Gaidahawa", "Kanchan", "Kotahimai",
"Marchawari", "Mayadevi", "Omsatiya", "Rohini", "Sammarimai",
"Siyari", "Sudhdhodhan", "Devdaha", "Lumbini Sanskritik", "Sainamaina",
"Siddharthanagar", "Tillotama", "Butwal"), Total.Area..ha. = c(9657L,
5835L, 5812L, 4844L, 7228L, 4844L, 6449L, 5066L, 6620L, 5743L,
13667L, 11194L, 16082L, 3595L, 12592L, 10139L), Forest.Area.ha. = c(114.91,
178.19, 31.37, 43.43, 152.87, 29.12, 63.16, 59.81, 36.4, 16.42,
113.13, 422.87, 186.13, 167.2, 60.27, 45.3), Forest.Percent = c(6.67,
10.35, 1.83, 2.52, 8.88, 1.69, 3.67, 3.47, 2.11, 0.95, 6.57,
24.57, 10.81, 9.71, 3.5, 2.63), Forest.Area..Fraction. = c(0.07,
0.1, 0.02, 0.03, 0.09, 0.02, 0.04, 0.03, 0.02, 0.01, 0.07, 0.25,
0.11, 0.1, 0.04, 0.03), Household.No = c(8612L, 9828L, 5939L,
5305L, 8003L, 6683L, 6349L, 5164L, 7889L, 7619L, 15624L, 10736L,
17572L, 12329L, 30452L, 36989L), Family.Size = c(10020L, 10483L,
7921L, 6972L, 10040L, 8218L, 8096L, 7303L, 9060L, 8717L, 17582L,
13854L, 19657L, 16011L, 36399L, 51099L), Total = c(56529L, 42528L,
46417L, 41058L, 57341L, 41080L, 43277L, 43300L, 45274L, 41472L,
71806L, 88090L, 78477L, 76307L, 149657L, 195054L)), row.names = c(NA,
16L), class = "data.frame")
The code I used is:-
setwd("C:/Users/lenovo/Desktop/AllAboutR/AssignmentDocs")
ForestArea2010<-read.csv("Forest2010.csv")
View(ForestArea2010)
pie(RupandehiLULC19$GaPa_NaPa, main="Piechart of Forest Area", las=3, col=hsv(12))
But I couldn't work further on how to show piechart showing Forest.Area.ha. by GaPa_NaPa working on code to plot piechart. Please help on it. How the code must be written?
Here is another option with ggplot2. If you don't want the labels then the geom_text_repel line can be removed.
library(ggplot2)
library(ggrepel)
ggplot(RupandehiLULC19, aes(x = "", y = `Forest.Area.ha.`, fill = GaPa_NaPa)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0) +
xlab("") +
ylab("Piechart of Forest Area") +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
geom_text_repel(aes(label = `Forest.Area.ha.`),
position = position_stack(vjust = 0.5))
Output
Or another option using legend:
pie(RupandehiLULC19$Forest.Area.ha., labels = "", main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")
Or with the values:
pie(RupandehiLULC19$Forest.Area.ha., labels = RupandehiLULC19$Forest.Area.ha., main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")

Colour a Q-Q plot comparing two distributions by quartiles in R

I am trying to construct a Q-Q plot comparing two distributions, with the 99th percentile colored like the following example:
However I am not sure how to achieve this, here is a subset of my data:
dfw <- structure(list(Date.Time = structure(c(848502000, 848509200,
848512800, 848520000, 848523600, 848530800, 848534400, 848541600,
848545200, 848552400, 848556000, 848563200, 848566800, 848574000,
848577600, 848588400, 848595600, 848599200, 848606400, 848610000,
848617200, 848620800, 848628000, 848631600, 848638800, 848642400,
848649600, 848653200, 848660400, 848664000, 848674800, 848682000,
848685600, 848692800, 848696400, 848703600, 848707200, 848714400,
848718000, 848725200, 848728800, 848736000, 848739600, 848746800,
848750400, 848761200, 848768400, 848772000, 848779200, 848782800,
848790000, 848793600, 848800800, 848804400, 848811600, 848815200,
848822400, 848826000, 848833200, 848847600, 848854800, 848858400,
848865600, 848869200, 848876400, 848880000, 848887200, 848890800,
848898000, 848901600, 848908800, 848912400, 848919600, 848923200,
848934000, 848941200, 848944800, 848952000, 848955600, 848962800,
848966400, 848973600, 848977200, 848984400, 848988000, 848995200,
848998800, 849006000, 849009600, 853682400, 853686000, 853714800,
853718400, 853725600, 853729200, 853736400, 853750800, 853758000,
853761600, 853768800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Hs.Mod = c(1.5960001, 1.5600001, 1.5480001, 1.552, 1.552,
1.534, 1.5180001, 1.462, 1.4260001, 1.3740001, 1.36, 1.3340001,
1.3080001, 1.2720001, 1.256, 1.218, 1.212, 1.21, 1.2060001,
1.2160001, 1.248, 1.25, 1.25, 1.264, 1.3560001, 1.394, 1.3700001,
1.332, 1.2900001, 1.2800001, 1.268, 1.2800001, 1.2800001,
1.3240001, 1.3240001, 1.286, 1.2540001, 1.19, 1.172, 1.1700001,
1.1860001, 1.2340001, 1.274, 1.3640001, 1.4120001, 1.58,
1.6580001, 1.6660001, 1.682, 1.748, 1.9280001, 1.9800001,
2.026, 2.052, 2.214, 2.328, 2.4320002, 2.39, 2.2180002, 1.9080001,
1.792, 1.7400001, 1.7140001, 1.7140001, 1.692, 1.6680001,
1.608, 1.58, 1.536, 1.524, 1.5640001, 1.5760001, 1.6100001,
1.6240001, 1.6120001, 1.5920001, 1.58, 1.542, 1.5200001,
1.48, 1.4640001, 1.4260001, 1.406, 1.386, 1.3820001, 1.34,
1.312, 1.268, 1.248, 1.8080001, 1.7960001, 1.644, 1.6420001,
1.6600001, 1.6880001, 1.7820001, 2.138, 2.2740002, 2.2940001,
2.252), Hs.Obs = c(1.741, 1.524, 1.618, 1.658, 1.697, 1.822,
1.792, 1.463, 1.433, 1.376, 1.208, 1.299, 1.255, 1.304, 1.328,
1.182, 1.282, 1.293, 1.228, 1.281, 1.45, 1.356, 1.501, 1.5,
1.356, 1.477, 1.408, 1.544, 1.497, 1.768, 2.04, 2.074, 2.042,
2.147, 2.224, 2.022, 2.017, 2.047, 2.353, 2.597, 2.838, 2.67,
2.762, 2.687, 2.734, 2.738, 2.938, 2.795, 2.549, 2.669, 2.447,
2.676, 2.577, 2.383, 2.362, 2.284, 2.341, 2.33, 2.397, 2.498,
2.317, 2.373, 2.377, 2.362, 2.218, 2.226, 1.97, 2.087, 1.874,
2.116, 2.022, 1.886, 2.046, 1.879, 1.638, 1.677, 1.638, 1.647,
1.551, 1.596, 1.591, 1.384, 1.345, 1.522, 1.469, 1.503, 1.459,
1.327, 1.453, 2.448, 2.235, 2.104, 1.958, 2.118, 2.209, 2.034,
2.229, 2.505, 2.163, 2.372)), row.names = c(NA, 100L), class = "data.frame")
Code to make the Q-Q plot:
ggplot(data=dfw, aes(x=sort(Hs.Obs), y=sort(Hs.Mod))) + geom_point(shape = 1, size =2) + xlab('Obs') + ylab('Model')+
theme_bw()+
geom_abline(linetype=2)
Code attempting to colour the 99th percentile:
ggplot(data=dfw, aes(x=sort(Hs.Obs), y=sort(Hs.Mod), col=cut(Hs.Mod,quantile(Hs.Mod, probs = .99)))) +
geom_point(shape = 1, size =2) + xlab('Obs') + ylab('Model')+
theme_bw()+
geom_abline(linetype=2)
Resulting plot:
I'm looking for some help to sort this out, as the attempts I have tried aren't working.
Thanks in advance!
try this
dfw %>%
mutate(qq = quantile(Hs.Mod, probs = c(0.99)),
qq_gt99 = ifelse(qq<= Hs.Mod, 1, 0)) %>%
ggplot(aes(x=Hs.Mod, y = Hs.Obs, col= as.factor(qq_gt99))) + geom_point()
if you order the observations first
dfw %>%
mutate(mod_ordered = sort(Hs.Mod),
obs_ordered = sort(Hs.Obs),
qq = quantile(mod_ordered, probs = c(0.99)),
qq_gt99 = ifelse(qq<= mod_ordered, 1, 0)) %>%
ggplot(aes(x=mod_ordered, y = obs_ordered, col=
as.factor(qq_gt99))) + geom_point()
Im using the dplyr library to filter() the data by quantile():
The code:
library(dplyr)
library(ggplot2)
ggplot()+
geom_point(data=filter(dfw,Hs.Obs>quantile(dfw$Hs.Mod,.99)),aes(x=sort(Hs.Obs),y=sort(Hs.Mod), col="Cuantile 99%"))+
geom_point(data=filter(dfw,Hs.Obs<quantile(dfw$Hs.Mod,.99)),aes(x=sort(Hs.Obs),y=sort(Hs.Mod), col="Cuantile 1-98%"))+
geom_abline(linetype=2)+xlab('Obs') + ylab('Model')+ theme_bw()

ggplot counting observations in each quadrant

I have some data as below:
# A tibble: 158 x 2
X Y
<dbl> <dbl>
1 -0.71 -2.39
2 0.92 0.573
3 -2.52 -1.61
4 3.88 5.43
5 0.15 0.106
6 3.49 6.66
7 -0.54 0.613
8 1.4 4.21
9 1.16 0.107
10 -3.37 -3.62
# ... with 148 more rows
I plot the observations and draw a line horizontally and vertically at 0 using:
df %>%
ggplot(aes(x = X,
y = Y)) +
geom_point() +
#geom_smooth(method = "lm") +
geom_hline(aes(yintercept = 0)) +
geom_vline(aes(xintercept = 0))
What I would like to do now is count the number of points which are in each quadrant of the plot and just display the number on each of the quadrant.
Data:
df <- structure(list(X = c(-0.71, 0.92, -2.52, 3.88, 0.15, 3.49, -0.54,
1.4, 1.16, -3.37, -0.55, -0.74, 2.13, 1.33, 3.27, 1.74, 0.65,
1.23, -1.75, 0.9, 3.86, 3.69, -1.74, -3.43, 0.67, 3.83, 2.32,
-5.46, -0.55, -6.39, -2.23, -1.3, 4.72, 2.42, -7.9, -1.54, 0.99,
-9.97, -18.41, -7.73, 1.5, -7.5, -9.88, 8.82, 10.48, 6.7, -0.23,
8.15, 3.02, 4.54, -2.76, 5.77, 3.03, -3.63, 3.71, 6.27, 1.92,
-7.86, -5.5, -4.44, 9.47, 3.89, 0.81, 6.83, 1.98, 4.01, 0.43,
2.79, -1.48, -1.87, -5.93, -8.58, 11.56, -0.46, 0.33, 5.27, 4.32,
2.4, -0.64, -6.7, 3.74, 1.01, 2.76, 2.8, -1.63, 0.65, 1.3, 5.33,
0.96, 3.71, 1.27, 2.53, -1.52, 5.69, -2.53, 3.82, 4.09, 2.79,
2.64, -3.42, 4.72, 0.62, 0.25, 1.98, 2.82, -2.06, 4.06, -2.45,
2.03, 2.22, -0.2, -3.47, 6.15, -1.2, 1.11, 1, -1.71, 1.05, -5.93,
-3.35, 7.53, 0.45, -2.45, -5.73, 0.26, 7, 1.12, 1.39, -0.11,
0.43, 0.34, -2.05, 4.54, 1.76, 2.15, 3.26, 0.2, 0.84, 0.93, 0.98,
1.97, 0.07, 2.48, 1.98, 2.88, 1.18, 5.23, -3.95, -2.17, 0.35,
2.51, 0.39, 3.11, 3.09, 0.06, -7.81, 1.62, -9.53), Y = c(-2.38916419707325,
0.572675136581781, -1.61130358515631, 5.42706994951004, 0.105533424368025,
6.65697289481407, 0.613486039256266, 4.21013704773222, 0.106990463992386,
-3.62352710962904, -0.203607589793183, -4.24563967581072, 2.97070300267885,
2.92544516479698, 5.02538739147422, 2.25461465260415, 1.66492554339803,
3.5690423154001, 0.108411247307002, 0.961008630173696, 3.79172784045593,
1.94108347244724, -2.12992072359958, -5.87473482253699, -1.45100684091412,
1.47842234462587, 1.43196010231586, -7.74290369146724, -2.79056547363334,
-5.03532133668577, -1.99400739381075, -2.92320856826413, 3.93394610595585,
3.29451174347621, -10.0410470556235, 3.34517672842812, 2.41625183369762,
-10.3476519710384, -21.791966984666, -11.1142687331988, 3.32761656369176,
-3.96223311815655, -11.093184503697, 11.6694167237026, 22.2461574652919,
9.28255170483023, 4.63817899423635, 11.8553670456421, 8.27889381692159,
8.19911670446593, -6.470817611772, 3.09218109975165, 7.5825172514382,
0.0284717847140023, 4.90864483240255, 10.0311544305095, 8.55401150272708,
-8.84107625063785, -8.04105369987643, -6.65872061590883, 10.8577722872979,
4.03706922467202, 3.04148092466194, 8.90634921641063, 1.56555573277521,
4.42535372370123, 0.841035482771217, 1.75578768128183, -2.67241757153407,
-2.25418139889371, -8.7723458397205, -11.2420616969584, 11.4836809985778,
-1.8649021388476, 0.832085873992507, 11.6062841497052, 2.59039949751966,
2.28509371230735, -1.97715071813135, -7.3280081242774, 3.97121830333205,
-0.569284938256821, 2.31082313266322, 3.02490478503254, -1.38512132143018,
-0.866847983058995, 2.97552563660034, 5.95976111047322, -0.102502393594657,
4.58003409048615, 0.842834319309465, 3.06786040532266, 0.250639945095402,
6.78696057469418, -1.62606880448011, 5.46367912370997, 2.53357559730344,
4.73895950607308, 2.50934817572881, -0.312149263565189, 4.82621271905962,
-0.79009628184665, -3.12115495501355, -0.461711220579862, 4.27359516836912,
-4.60871127364226, 3.84488020178729, -5.26245849925393, 3.54222359765326,
1.04191534953213, 1.4982293818719, -3.56618092951384, 4.95478586278666,
-0.270584959088251, -0.900452947549406, 0.901254072925249, -0.254483190258712,
-2.63217404877559, -4.71624328721887, -7.1747474980974, 4.86036342835152,
3.24549729559669, -4.19219918146311, -10.128570960197, 0.803895306904637,
9.33865112323734, 2.85517888612945, 0.316844258915139, -0.151669189522978,
1.00839469793829, 1.57398998124214, -5.0607247073979, 8.91704977465508,
2.59984205825244, 1.31737969318745, 2.70804837397023, 1.80193676584248,
1.48362026996833, -2.11380109244311, 3.54300752215851, 1.6501194298151,
-1.01504840432201, 6.74326962933175, 0.1866931051541, 2.9825290286452,
1.42593783576641, 2.71110274944611, -4.09572797775837, 1.50144422897237,
-0.552818435076999, 5.23843746771127, 1.33321908169899, 1.28745947800351,
2.60490918566195, -1.54038908822145, -9.6363012621261, -0.190177144865133,
-13.0653210889016)), row.names = c(NA, -158L), class = c("tbl_df",
"tbl", "data.frame"))
library(dplyr)
quad_count <- df %>%
# Count how many with each combination of X and Y being positive
count(right = X > 0, top = Y > 0) %>%
# TRUE = 1, FALSE = 0, so these map the TRUE to +1 and FALSE to -1
mutate(X = 2 * (right - 0.5), Y = 2 * (top - 0.5))
df %>%
ggplot(aes(x = X, y = Y)) +
geom_point() +
geom_hline(aes(yintercept = 0)) +
geom_vline(aes(xintercept = 0)) +
# This layer should use the other dataset, but keep using X and Y for location
geom_text(data = quad_count, aes(label = n), size = 10)
df %>%
ggplot(aes(x = X,
y = Y)) +
geom_point() +
#geom_smooth(method = "lm") +
geom_hline(aes(yintercept = 0)) +
geom_vline(aes(xintercept = 0)) +
geom_text(data = df %>%
mutate(X = X >= 0, Y = Y >= 0) %>%
count(X, Y) %>%
mutate(X = if_else(X, 10, -10),
Y = if_else(Y, 10, -10)),
mapping = aes(X, Y, label = n), size = 10)

how do you draw 3d charts in R with different colors

I have this data frame called t:
dput(t)
structure(list(timestamp = structure(c(1466306383, 1466306445,
1466306507, 1466306569, 1466306631, 1466306693, 1466306755, 1466306817,
1466306879, 1466306943, 1466307006, 1466307068, 1466307130, 1466307193,
1466307255, 1466307317, 1466307379, 1466307442, 1466307504, 1466307566
), class = c("POSIXct", "POSIXt"), tzone = ""), cpuused = c(1.13007,
1.13007, 1.13002, 1.12996, 1.1299, 1.12985, 1.12979, 1.12976,
1.1297, 1.12965, 1.12959, 1.12953, 1.12947, 1.12942, 1.12936,
1.1293, 1.12927, 1.12921, 1.12915, 1.1291), transratepersec = c(2640.77,
2640.61, 2640.44, 2640.28, 2640.12, 2639.95, 2639.79, 2639.69,
2639.53, 2639.36, 2639.19, 2639.03, 2638.86, 2638.7, 2638.54,
2638.37, 2638.21, 2638.04, 2637.88, 2637.72), reqpersec = c(0.172818,
0.172806, 0.172793, 0.172779, 0.172766, 0.172752, 0.172739, 0.172727,
0.172714, 0.1727, 0.172687, 0.172673, 0.17266, 0.172646, 0.172633,
0.17262, 0.172608, 0.172594, 0.172581, 0.172567), resptime = c(0.274,
0.235, 0.234, 0.234, 0.236, 0.234, 0.235, 0.236, 0.236, 0.233,
0.267, 0.235, 0.243, 0.235, 0.232, 0.233, 0.31, 0.233, 0.26,
0.234)), .Names = c("timestamp", "cpuused", "transratepersec",
"reqpersec", "resptime"), row.names = c(11653L, 19385L, 2624L,
16106L, 13990L, 12724L, 9490L, 12720L, 19387L, 11656L, 13988L,
9488L, 11650L, 5639L, 16104L, 15090L, 17156L, 4856L, 12722L,
382L), class = "data.frame")
I would like to create a 3 dimensional chart with x=reqpersec, y=cpuused, z=transrateperse, having different colors of x,y and x,z.
I have tried this:
library(rgl)
attach(t)
plot3d(x=reqpersec, y=cpuused, z=transratepersec, type="p", col="red", xlab="ReqPerSec", ylab="CPU", zlab="TransRate", size=5, lwd=15, box=F)
It is only giving me one color. Any ideas how to easily do this?
I would like images to be similar to this:

Resources