Align two legends in one plot - r

I would like to add legends for two geom_line and a geom_point at the same time, but the legends were not align to each other. So how to align the two legends and adjust legend positions? Thank you in advance!
My data:
df1:
x1 y1
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 9.2
24 18.5
25 27.6
26 36.8
27 46.1
28 54.2
29 63.4
30 72.6
31 81.7
32 88.9
33 93
34 99.1
35 105.4
36 110
37 118.3
38 128.2
39 138
40 146.9
41 155.1
42 162.5
43 165.7
44 169.2
45 174.2
46 176.3
47 183.8
48 187.8
49 194.2
50 200.7
51 203.4
52 204.7
53 209.5
54 214.5
55 219.6
56 224.1
57 228.5
58 232.8
59 237
60 239.5
61 242.7
62 243.1
63 244.6
64 245
65 246.6
66 248.6
67 251
68 253
69 255
70 256.7
71 256.7
df2:
x2 y2
24 0.006525
32 0.072525
39 0.120025
46 0.1601418
53 0.1972939
60 0.2226233
68 0.2312895
df3:
x3 y3
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 10.9
24 14.8
25 19.6
26 25.6
27 31.4
28 38.5
29 47.1
30 56.9
31 64.7
32 71
33 77
34 84.7
35 92.5
36 98.8
37 108.2
38 118.8
39 126.9
40 134.3
41 141.1
42 147.2
43 149.9
44 152.8
45 157
46 158.7
47 164.9
48 168.3
49 173.6
50 179
51 181.3
52 182.3
53 186.3
54 190.4
55 194.7
56 198.5
57 202.1
58 205.7
59 209.2
60 211.3
61 213.9
62 214.3
63 215.6
64 215.9
65 217.2
66 218.9
67 220.9
68 222.5
69 224.2
70 225.7
71 225.7
My code:
library("ggplot2")
library("reshape2")
library("gridExtra")
p <- ggplot() +
geom_line(data=df1, aes(x= x1, y= y1, linetype= "aa"))+
geom_point(data=df2, aes(x= x2, y= y2, shape="bbbbbbb"))+
geom_line(data=df3, aes(x= x3, y= y3, linetype= "cc"))+
scale_shape_manual(name="",
labels=c("bbbbbbb"),
values = c(21) )+
scale_linetype_manual(name="",
labels=c("aa","cc"),
values=c("solid", "dashed")) +
ylab("y")+
xlab("x")+
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.justification = c(0, 1),
legend.position=c(0, 1))
My plot:

Thank you guys for attention. I have find a solution to the problem, I adopted the idea from this post.
library("ggplot2")
library("reshape2")
library("gridExtra")
library("gtable")
p <- ggplot() +
geom_line(data=df1, aes(x= x1, y= y1, linetype= "aa"))+
geom_point(data=df2, aes(x= x2, y= y2, shape="bbbbbbb"))+
geom_line(data=df3, aes(x= x3, y= y3, linetype= "cc"))+
# discard errorbar here.
scale_shape_manual(name=NULL,
labels=c("bbbbbbb"),
values = c(21) )+
scale_linetype_manual(name=NULL,
labels=c("aa","cc"),
values=c("solid", "dashed")) +
ylab("y")+
xlab("x")+
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position=c(0, 1),
legend.justification=c(0,1),
legend.margin=unit(0,"cm"),
legend.box="vertical",
legend.box.just = "left",
legend.key.size=unit(1,"lines"),
legend.text.align=0,
legend.key = element_blank(),
legend.title = element_blank(),
legend.background=element_blank())
data <- ggplot_build(p)
gtable <- ggplot_gtable(data)
lbox <- which(sapply(gtable$grobs, paste) == "gtable[guide-box]")
guide <- gtable$grobs[[lbox]]
gtable$grobs[[lbox]]$heights <- unit.c(guide$heights[1:2],
unit(-.8,"cm"),
guide$heights[2:3])
# Plotting
g<-grid.draw(gtable)

Related

How to plot two columns in one graph depending on rowid in R

I would like to use in ploting only some columns that are in my dataframe, but i don't know how and i can't exclude rows that haven't take a part in plotting.
That is how data looks like:
txt <- "
PM10 PM25 Month row color
1 73 15.5 1 1 black
2 57 12.3 1 2 black
3 22 5.8 1 3 black
4 16 5.2 1 4 black
5 32 9.3 1 5 black
6 42 8.5 1 6 black
7 120 21.5 1 7 black
8 70 18.1 1 8 black
9 14 6.2 1 9 black
10 12 5.0 1 10 black
11 16 4.5 1 11 black
12 15 5.2 1 12 black
13 40 6.4 1 13 black
14 61 10.0 1 14 black
15 74 9.5 1 15 black
16 145 12.8 1 16 black
17 58 6.7 1 17 black
18 36 4.6 1 18 black
19 35 2.8 1 19 black
20 43 4.5 1 20 black
21 73 7.5 1 21 black
22 40 3.7 1 22 black
23 33 5.1 1 23 black
24 32 4.2 1 24 black
25 47 12.5 1 25 black
26 79 22.3 1 26 black
27 72 16.0 1 27 black
28 44 15.0 1 28 black
29 38 9.3 1 29 black
30 35 7.0 1 30 black
31 24 6.3 1 31 black
32 82 67.0 2 32 black
33 166 157.0 2 33 black
34 68 56.0 2 34 black
35 87 72.0 2 35 black
36 58 47.0 2 36 black
37 30 24.0 2 37 black
38 52 42.0 2 38 black
39 18 12.0 2 39 black
40 37 31.0 2 40 black
41 51 43.0 2 41 black
42 65 53.0 2 42 black
43 71 59.0 2 43 black
44 144 121.0 2 44 black
45 131 112.0 2 45 black
46 117 109.0 2 46 black
47 31 23.0 2 47 black
48 59 41.0 2 48 black
49 123 104.0 2 49 black
50 96 81.0 2 50 black
51 145 113.0 2 51 black
52 99 76.0 2 52 black
53 86 68.0 2 53 black
54 91 72.0 2 54 black
55 39 31.0 2 55 black
56 40 32.0 2 56 black
57 61 55.0 2 57 black
58 82 61.0 2 58 black
59 111 95.0 2 59 black
60 57 45.0 3 60 black
61 29 16.0 3 61 black
62 33 22.0 3 62 black
"
df <- read.table(textConnection(txt), header = TRUE)
Some code preparing my dataframe to exercise:
df <- data.frame(data)
df$Month <- as.numeric(format(data$date,format="%m"))
df <- df %>% select("PM10","PM25","Month") ##%>% mutate(row=row_number())
df$color <- 'black'
The problem is that i can plot them easily in two different plots using just
plot (df$PM10)
par(new=TRUE)
plot (df$PM25)
but i need to have it in one...
When im plotting it this way:
plot(df)
it taking every single column to plot, I just want to show PM10 and PM25, as a values that will be shown in 'pairs' for each rowid.
So simply I want to have plot, with PM10 points and PM25 points and every point will be attached to rowid. How can I do this?
If you are using package dplyr to prepare the data, try using package ggplot2 to plot the graph.
I have added some rows with Month == 12, since there were none in the question's data. See at end.
df2 <- df %>%
mutate(color = ifelse(Month == 12, "red", as.character(color)),
color = factor(color, labels = c("Other", "12")),
x = row_number())
ggplot(df2, aes(x = x, colour = color)) +
geom_point(aes(y = PM10)) +
geom_point(aes(y = PM25)) +
scale_color_manual(name = "Month", values = unique(df2$color))
New data with Month == 12.
sp <- split(df, df$Month)[[1]]
sp[['Month']] <- 12
sp[['PM10']] <- jitter(sp[['PM10']])
sp[['PM25']] <- jitter(sp[['PM25']])
df <- rbind(df, sp)

Why no output for geom_boxplot()?

I am trying to plot a box plot for the data frame below. Running the code below seems doesn't work, there is no output at all.
After running
ggplot(aes(time, size)) +
somehow the dataframe was changed to a list. Anyone helps?
SUBJECTID Baseline 1 2 3
1001 88 78 30 14
1002 29 26 66 16
1003 50 64 54 46
1004 91 90 99 43
1005 98 109 60 42
1007 100 100 54
1008 45 49 47 32
1009 75 66 57 7
1010 60 52 20 3
1011 68 68 56 47
1012 78 84 56 57
1013 71 70 8 5
1015 79 50 11 3
1016 73 60 57 36
1017 54 27 16
1018 50 37 33 26
1019 115 68 33 67
1021 63 55 0 0
1022 98 91 76 75
1024 76 76 0
1025 47 45 42 42
1026 32 25 14 0
1027 40 37 65
1028 60 110 110 0
tumor.df <- clinical %>%
select(SUBJECTID, `MRI LD Baseline`,`MRI LD 1-3dAC`,
`MRI LD InterReg`,`MRI LD PreSurg`) %>%
rename(baseline = 'MRI LD Baseline', t1 = 'MRI LD 1-3dAC',
t2 = 'MRI LD InterReg', t3 = 'MRI LD PreSurg') %>%
gather(time, size, baseline : t3) %>%
ggplot(aes(time, size)) +
geom_boxplot()
This is the output I get from the following code:
clinical <- read.table(text="
SUBJECTID baseline t1 t2 t3
1001 88 78 30 14
1002 29 26 66 16
1003 50 64 54 46
1004 91 90 99 43
1005 98 109 60 42
1007 100 100 NA 54
1008 45 49 47 32
1009 75 66 57 7
1010 60 52 20 3
1011 68 68 56 47
1012 78 84 56 57
1013 71 70 8 5
1015 79 50 11 3
1016 73 60 57 36
1017 54 27 16 NA
1018 50 37 33 26
1019 115 68 33 67
1021 63 55 0 0
1022 98 91 76 75
1024 76 76 NA 0
1025 47 45 42 42
1026 32 25 14 0
1027 40 37 65 NA
1028 60 110 110 0
", header=T)
library(ggplot2)
library(tidyr)
library(plyr)
clinical %>%
rename(c(baseline = 'MRI LD Baseline', t1 = 'MRI LD 1-3dAC',
t2 = 'MRI LD InterReg', t3 = 'MRI LD PreSurg')) %>%
gather(time, size, 'MRI LD Baseline':'MRI LD PreSurg') %>%
mutate(time = factor(time, levels=unique(time))) %>%
ggplot(aes(time, size)) +
geom_boxplot()

Plotting Partial Least Squares Regression (plsr) biplot with ggplot2

Using the data.frame below (Source: http://eric.univ-lyon2.fr/~ricco/tanagra/fichiers/en_Tanagra_PLSR_Software_Comparison.pdf)
Data
df <- read.table(text = c("
diesel twodoors sportsstyle wheelbase length width height curbweight enginesize horsepower horse_per_weight conscity price symboling
0 1 0 97 172 66 56 2209 109 85 0.0385 8.7 7975 2
0 0 0 100 177 66 54 2337 109 102 0.0436 9.8 13950 2
0 0 0 116 203 72 57 3740 234 155 0.0414 14.7 34184 -1
0 1 1 103 184 68 52 3016 171 161 0.0534 12.4 15998 3
0 0 0 101 177 65 54 2765 164 121 0.0438 11.2 21105 0
0 1 0 90 169 65 52 2756 194 207 0.0751 13.8 34028 3
1 0 0 105 175 66 54 2700 134 72 0.0267 7.6 18344 0
0 0 0 108 187 68 57 3020 120 97 0.0321 12.4 11900 0
0 0 1 94 157 64 51 1967 90 68 0.0346 7.6 6229 1
0 1 0 95 169 64 53 2265 98 112 0.0494 9.0 9298 1
1 0 0 96 166 64 53 2275 110 56 0.0246 6.9 7898 0
0 1 0 100 177 66 53 2507 136 110 0.0439 12.4 15250 2
0 1 1 94 157 64 51 1876 90 68 0.0362 6.4 5572 1
0 0 0 95 170 64 54 2024 97 69 0.0341 7.6 7349 1
0 1 1 95 171 66 52 2823 152 154 0.0546 12.4 16500 1
0 0 0 103 175 65 60 2535 122 88 0.0347 9.8 8921 -1
0 0 0 113 200 70 53 4066 258 176 0.0433 15.7 32250 0
0 0 0 95 165 64 55 1938 97 69 0.0356 7.6 6849 1
1 0 0 97 172 66 56 2319 97 68 0.0293 6.4 9495 2
0 0 0 97 172 66 56 2275 109 85 0.0374 8.7 8495 2"), header = T)
and this
Code
library(pls)
Y <- as.matrix(df[,14])
X <- as.matrix(df[,1:11])
df.pls <- mvr(Y ~ X, ncomp = 3, method = "oscorespls", scale = T)
plot(df.pls, "biplot")
I got this
Biplot
Any help to plot the pls biplot using ggplot2 will be appreciated?
#Read data
df <- read.table(text = c("
diesel twodoors sportsstyle wheelbase length width height curbweight enginesize horsepower horse_per_weight conscity price symboling
0 1 0 97 172 66 56 2209 109 85 0.0385 8.7 7975 2
0 0 0 100 177 66 54 2337 109 102 0.0436 9.8 13950 2
0 0 0 116 203 72 57 3740 234 155 0.0414 14.7 34184 -1
0 1 1 103 184 68 52 3016 171 161 0.0534 12.4 15998 3
0 0 0 101 177 65 54 2765 164 121 0.0438 11.2 21105 0
0 1 0 90 169 65 52 2756 194 207 0.0751 13.8 34028 3
1 0 0 105 175 66 54 2700 134 72 0.0267 7.6 18344 0
0 0 0 108 187 68 57 3020 120 97 0.0321 12.4 11900 0
0 0 1 94 157 64 51 1967 90 68 0.0346 7.6 6229 1
0 1 0 95 169 64 53 2265 98 112 0.0494 9.0 9298 1
1 0 0 96 166 64 53 2275 110 56 0.0246 6.9 7898 0
0 1 0 100 177 66 53 2507 136 110 0.0439 12.4 15250 2
0 1 1 94 157 64 51 1876 90 68 0.0362 6.4 5572 1
0 0 0 95 170 64 54 2024 97 69 0.0341 7.6 7349 1
0 1 1 95 171 66 52 2823 152 154 0.0546 12.4 16500 1
0 0 0 103 175 65 60 2535 122 88 0.0347 9.8 8921 -1
0 0 0 113 200 70 53 4066 258 176 0.0433 15.7 32250 0
0 0 0 95 165 64 55 1938 97 69 0.0356 7.6 6849 1
1 0 0 97 172 66 56 2319 97 68 0.0293 6.4 9495 2
0 0 0 97 172 66 56 2275 109 85 0.0374 8.7 8495 2"), header = T)
#Run OP's code
library(pls)
library(ggplot2)
Y <- as.matrix(df[,14])
X <- as.matrix(df[,1:11])
df.pls <- mvr(Y ~ X, ncomp = 3, method = "oscorespls", scale = T)
#Extract information from mvr object
df2<-df.pls$scores
comp1a<-df2[,1]
comp2a<-df2[,2]
df2<-as.data.frame(cbind(comp1a, comp2a))
df1<-df.pls$loadings
comp1<-df1[,1]
comp2<-df1[,2]
names<-df1[,0]
df1<-as.data.frame(cbind(names, comp1, comp2))
#Generate two plots and overlay
#Plot 1
p1<-ggplot(data=df1, aes(comp1,comp2))+
ylab("")+xlab("")+ggtitle("X scores and X Loadings")+
theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1),panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"))+
geom_text(aes(label=rownames(df1)), color="red")+
scale_x_continuous(breaks = c(-0.6,-0.4,-0.2,0,0.2,0.4,0.6))+
scale_y_continuous(breaks = c(-0.6,-0.4,-0.2,0,0.2,0.4,0.6))+
coord_fixed(ylim=c(-0.6, 0.6),xlim=c(-0.6, 0.6))+
theme(axis.ticks = element_line(colour = "red")) +
theme(axis.text.y=element_text(angle = 90, hjust = 0.65)) +
theme(axis.text.y = element_text(margin=margin(10,10,10,5,"pt")))
#Plot 2
p2<-ggplot(data=df2, aes(comp1a,comp2a))+
ylab("Comp 2")+xlab("Comp 1")+ggtitle("X scores and X Loadings")+
theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"))+
geom_text(aes(label=rownames(df2)))+
xlim(-4,4)+ylim(-4,4)+
scale_y_continuous(breaks = c(-4,-2,0,2))+
coord_cartesian(ylim=c(-4, 4))+
scale_x_continuous(breaks = c(-4,-2,0,2)) +
theme(plot.title = element_text(face="bold"))+
theme(axis.text.y=element_text(angle = 90, hjust = 0.65))
#Function to overlay plots in order to get two graphs with different axes on same plot
library(grid)
library(gtable)
ggplot_dual_axis = function(plot1, plot2, which.axis = "x") {
# Update plot with transparent panel
plot2 = plot2 + theme(panel.background = element_rect(fill = NA))
grid.newpage()
# Increase right margin if which.axis == "y"
if(which.axis == "y") plot1 = plot1 + theme(plot.margin = unit(c(0.7, 1.5, 0.4, 0.4), "cm"))
# Extract gtable
g1 = ggplot_gtable(ggplot_build(plot1))
g2 = ggplot_gtable(ggplot_build(plot2))
# Overlap the panel of the second plot on that of the first
pp = c(subset(g1$layout, name == "panel", se = t:r))
g = gtable_add_grob(g1, g2$grobs[[which(g2$layout$name=="panel")]], pp$t, pp$l, pp$b, pp$l)
# Steal axis from second plot and modify
axis.lab = ifelse(which.axis == "x", "axis-b", "axis-l")
ia = which(g2$layout$name == axis.lab)
ga = g2$grobs[[ia]]
ax = ga$children[[2]]
# Switch position of ticks and labels
if(which.axis == "x") ax$heights = rev(ax$heights) else ax$widths = rev(ax$widths)
ax$grobs = rev(ax$grobs)
if(which.axis == "x")
ax$grobs[[2]]$y = ax$grobs[[2]]$y - unit(1, "npc") + unit(0.15, "cm") else
ax$grobs[[1]]$x = ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
# Modify existing row to be tall enough for axis
if(which.axis == "x") g$heights[[2]] = g$heights[g2$layout[ia,]$t]
# Add new row or column for axis label
if(which.axis == "x") {
g = gtable_add_grob(g, ax, 2, 4, 2, 4)
g = gtable_add_rows(g, g2$heights[1], 1)
g = gtable_add_grob(g, g2$grob[[6]], 2, 4, 2, 4)
} else {
g = gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g = gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
g = gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths), pp$b - 1)
}
# Draw it
grid.draw(g)
}
#Run function on individual plots
ggplot_dual_axis(p2, p1, "y")

Plot partial least squares regression biplot with ggplot2

Using the data.frame below (Source: http://eric.univ-lyon2.fr/~ricco/tanagra/fichiers/en_Tanagra_PLSR_Software_Comparison.pdf)
Data
df <- read.table(text = c("
diesel twodoors sportsstyle wheelbase length width height curbweight enginesize horsepower horse_per_weight conscity price symboling
0 1 0 97 172 66 56 2209 109 85 0.0385 8.7 7975 2
0 0 0 100 177 66 54 2337 109 102 0.0436 9.8 13950 2
0 0 0 116 203 72 57 3740 234 155 0.0414 14.7 34184 -1
0 1 1 103 184 68 52 3016 171 161 0.0534 12.4 15998 3
0 0 0 101 177 65 54 2765 164 121 0.0438 11.2 21105 0
0 1 0 90 169 65 52 2756 194 207 0.0751 13.8 34028 3
1 0 0 105 175 66 54 2700 134 72 0.0267 7.6 18344 0
0 0 0 108 187 68 57 3020 120 97 0.0321 12.4 11900 0
0 0 1 94 157 64 51 1967 90 68 0.0346 7.6 6229 1
0 1 0 95 169 64 53 2265 98 112 0.0494 9.0 9298 1
1 0 0 96 166 64 53 2275 110 56 0.0246 6.9 7898 0
0 1 0 100 177 66 53 2507 136 110 0.0439 12.4 15250 2
0 1 1 94 157 64 51 1876 90 68 0.0362 6.4 5572 1
0 0 0 95 170 64 54 2024 97 69 0.0341 7.6 7349 1
0 1 1 95 171 66 52 2823 152 154 0.0546 12.4 16500 1
0 0 0 103 175 65 60 2535 122 88 0.0347 9.8 8921 -1
0 0 0 113 200 70 53 4066 258 176 0.0433 15.7 32250 0
0 0 0 95 165 64 55 1938 97 69 0.0356 7.6 6849 1
1 0 0 97 172 66 56 2319 97 68 0.0293 6.4 9495 2
0 0 0 97 172 66 56 2275 109 85 0.0374 8.7 8495 2"), header = T)
and this
Code
library(plsdepot)
df.plsdepot = plsreg1(df[, 1:11], df[, 14, drop = FALSE], comps = 3)
plot(df.plsdepot, comps = c(1, 2))
I got this
Result
The dependent (y) variable here is symboling, like price, is function of all other independent variables for the cars (diesel, twodoors, sportsstyle, wheelbase, length, width, height, curbweight, enginesize,horsepower, horse_per_weight)
Question
Any help to create the plot above using ggplot2 but with arrows instead of lines similar to this plot will be highly appreciated?
df <- read.table(text = c("
diesel twodoors sportsstyle wheelbase length width height curbweight enginesize horsepower horse_per_weight conscity price symboling
0 1 0 97 172 66 56 2209 109 85 0.0385 8.7 7975 2
0 0 0 100 177 66 54 2337 109 102 0.0436 9.8 13950 2
0 0 0 116 203 72 57 3740 234 155 0.0414 14.7 34184 -1
0 1 1 103 184 68 52 3016 171 161 0.0534 12.4 15998 3
0 0 0 101 177 65 54 2765 164 121 0.0438 11.2 21105 0
0 1 0 90 169 65 52 2756 194 207 0.0751 13.8 34028 3
1 0 0 105 175 66 54 2700 134 72 0.0267 7.6 18344 0
0 0 0 108 187 68 57 3020 120 97 0.0321 12.4 11900 0
0 0 1 94 157 64 51 1967 90 68 0.0346 7.6 6229 1
0 1 0 95 169 64 53 2265 98 112 0.0494 9.0 9298 1
1 0 0 96 166 64 53 2275 110 56 0.0246 6.9 7898 0
0 1 0 100 177 66 53 2507 136 110 0.0439 12.4 15250 2
0 1 1 94 157 64 51 1876 90 68 0.0362 6.4 5572 1
0 0 0 95 170 64 54 2024 97 69 0.0341 7.6 7349 1
0 1 1 95 171 66 52 2823 152 154 0.0546 12.4 16500 1
0 0 0 103 175 65 60 2535 122 88 0.0347 9.8 8921 -1
0 0 0 113 200 70 53 4066 258 176 0.0433 15.7 32250 0
0 0 0 95 165 64 55 1938 97 69 0.0356 7.6 6849 1
1 0 0 97 172 66 56 2319 97 68 0.0293 6.4 9495 2
0 0 0 97 172 66 56 2275 109 85 0.0374 8.7 8495 2"), header = T)
library(plsdepot)
library(ggplot2)
df.plsdepot = plsreg1(df[, 1:11], df[, 14, drop = FALSE], comps = 3)
data<-df.plsdepot$cor.xyt
data<-as.data.frame(data)
#Function to draw circle
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
dat <- circleFun(c(0,0),2,npoints = 100)
ggplot(data=data, aes(t1,t2))+
ylab("")+xlab("")+ggtitle("Circle of Correlations ")+
theme_bw() +geom_text(aes(label=rownames(data),
colour=ifelse(rownames(data)!='symboling', 'orange','blue')))+
scale_color_manual(values=c("orange","#6baed6"))+
scale_x_continuous(breaks = c(-1,-0.5,0,0.5,1))+
scale_y_continuous(breaks = c(-1,-0.5,0,0.5,1))+
coord_fixed(ylim=c(-1, 1),xlim=c(-1, 1))+xlab("axis 1")+
ylab("axis 2")+ theme(axis.line.x = element_line(color="darkgrey"),
axis.line.y = element_line(color="darkgrey"))+
geom_path(data=dat,aes(x,y), colour = "darkgrey")+
theme(legend.title=element_blank())+
theme(axis.ticks = element_line(colour = "grey"))+
theme(axis.title = element_text(colour = "darkgrey"))+
theme(axis.text = element_text(color="darkgrey"))+
theme(legend.position='none')+
theme(plot.title = element_text(color="#737373")) +
theme(panel.grid.minor = element_blank()) +
annotate("segment",x=0, y=0, xend= 0.60, yend= 0.20, color="orange",
arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= -0.25, yend= -0.35, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= 0.45, yend= 0.75, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= 0.37 , yend=-0.02, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= -0.80, yend= 0.30, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= -0.75, yend= 0.60, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= -0.67, yend= 0.60, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= -0.59, yend= -0.13, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= -0.59, yend= 0.70, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= -0.39, yend= 0.80, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= 0.04, yend= 0.93, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))+
annotate("segment",x=0, y=0, xend= 0.70, yend= 0.40, color="#6baed6",
alpha=0.3,arrow=arrow(length=unit(0.3,"cm")))

ggplot: Showing x-axis line for each facet plot

I have created a facet plot using ggplot which has 4 rows and 1 column with this code:
ggplot(data=c, aes(x=Time, y=X.mean, fill = Site, width=.1)) +
geom_bar(stat="identity", position=position_dodge(), width=0.5,colour="black", show_guide=FALSE) +
ylab(NULL) + xlab(NULL) +
geom_errorbar(aes(ymax= X.Passes.sd, ymin= 0),
size = 0.7, width = 0.3,position = position_dodge(0.9))+
scale_fill_manual(values=c("cadetblue2", "royalblue1", "mediumseagreen", "green4")) +
scale_y_continuous(breaks=number_ticks(12), expand = c(0, 0), limits=c(0,20)) +
scale_x_discrete(expand = c(0, 0), limits=c("17:00","18:00","19:00","20:00","21:00","22:00","23:00","00:00","01:00","02:00","03:00","04:00", "05:00", "06:00")) +
facet_wrap(~ Site, ncol=1,nrow=4)+
theme_bw() +
theme(strip.text.x = element_text(size=18))+
theme(axis.title.y=element_text(size = 18)) +
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())+
theme(axis.text.x=element_text(angle = 45, hjust=1, vjust=1, size = 18))+
theme(axis.text.y=element_text(size = 18))+
theme(panel.border = element_blank())+
theme(axis.line = element_line(color = 'black'))+
theme(axis.title.y=element_text(vjust=0.3, size=20))+
theme(strip.background = element_rect(colour="white", fill="white"))+
theme(legend.position = "none")
The issue I have is that only the bottom plot has a solid black line on the x-axis. The 3 plots above do not have this line and only have a dashed line for each data point.
Does anyone know how I can put a solid line on the x-axis for all of these plots?
Thanks
Jon
Edit 1: Data
Date Site Passes
02/11/2013 RM1 85
03/11/2013 RM1 254
04/11/2013 RM1 636
05/11/2013 RM1 610
06/11/2013 RM1 408
07/11/2013 RM1 293
08/11/2013 RM1 388
09/11/2013 RM1 513
10/11/2013 RM1 190
11/11/2013 RM1 333
12/11/2013 RM1 264
13/11/2013 RM1 261
14/11/2013 RM1 364
15/11/2013 RM1 1
16/11/2013 RM1 238
17/11/2013 RM1 149
18/11/2013 RM1 242
19/11/2013 RM1 225
20/11/2013 RM1 196
21/11/2013 RM1 68
22/11/2013 RM1 292
23/11/2013 RM1 159
24/11/2013 RM1 65
25/11/2013 RM1 166
26/11/2013 RM1 44
27/11/2013 RM1 0
28/11/2013 RM1 56
29/11/2013 RM1 378
30/11/2013 RM1 34
01/12/2013 RM1 43
02/12/2013 RM1 518
03/12/2013 RM1 286
04/12/2013 RM1 175
05/12/2013 RM1 169
06/12/2013 RM1 138
07/12/2013 RM1 445
08/12/2013 RM1 1153
09/12/2013 RM1 616
10/12/2013 RM1 1
02/11/2013 RM2 1
03/11/2013 RM2 30
04/11/2013 RM2 210
05/11/2013 RM2 47
06/11/2013 RM2 8
07/11/2013 RM2 66
08/11/2013 RM2 3
09/11/2013 RM2 7
10/11/2013 RM2 4
11/11/2013 RM2 13
12/11/2013 RM2 16
13/11/2013 RM2 31
14/11/2013 RM2 4
15/11/2013 RM2 0
16/11/2013 RM2 9
17/11/2013 RM2 24
18/11/2013 RM2 5
19/11/2013 RM2 47
20/11/2013 RM2 12
21/11/2013 RM2 3
22/11/2013 RM2 43
23/11/2013 RM2 8
24/11/2013 RM2 15
25/11/2013 RM2 26
26/11/2013 RM2 2
27/11/2013 RM2 0
28/11/2013 RM2 0
29/11/2013 RM2 9
30/11/2013 RM2 2
01/12/2013 RM2 1
02/12/2013 RM2 45
03/12/2013 RM2 26
04/12/2013 RM2 6
05/12/2013 RM2 8
06/12/2013 RM2 0
07/12/2013 RM2 0
08/12/2013 RM2 0
09/12/2013 RM2 0
10/12/2013 RM2 0
03/11/2013 RM3 14
04/11/2013 RM3 100
05/11/2013 RM3 22
06/11/2013 RM3 6
07/11/2013 RM3 35
08/11/2013 RM3 12
09/11/2013 RM3 30
10/11/2013 RM3 33
11/11/2013 RM3 3
12/11/2013 RM3 40
13/11/2013 RM3 88
14/11/2013 RM3 5
15/11/2013 RM3 10
16/11/2013 RM3 10
17/11/2013 RM3 13
18/11/2013 RM3 13
19/11/2013 RM3 20
20/11/2013 RM3 12
21/11/2013 RM3 3
22/11/2013 RM3 31
23/11/2013 RM3 1
24/11/2013 RM3 23
25/11/2013 RM3 11
26/11/2013 RM3 2
27/11/2013 RM3 0
28/11/2013 RM3 1
29/11/2013 RM3 23
30/11/2013 RM3 0
01/12/2013 RM3 0
02/12/2013 RM3 9
03/12/2013 RM3 19
04/12/2013 RM3 6
05/12/2013 RM3 8
06/12/2013 RM3 1
07/12/2013 RM3 1
08/12/2013 RM3 35
09/12/2013 RM3 7
10/12/2013 RM3 0
04/11/2013 RM4 371
05/11/2013 RM4 110
06/11/2013 RM4 36
07/11/2013 RM4 55
08/11/2013 RM4 45
09/11/2013 RM4 44
10/11/2013 RM4 10
11/11/2013 RM4 27
12/11/2013 RM4 86
13/11/2013 RM4 116
14/11/2013 RM4 55
15/11/2013 RM4 0
16/11/2013 RM4 95
17/11/2013 RM4 28
18/11/2013 RM4 50
19/11/2013 RM4 69
20/11/2013 RM4 51
You might have found a solution already, but I've always just included the line geom_hline(yintercept=0) to add the x-axis origin line to my plots. Its a serious hack, but it also provides you some aesthetic control, and I haven't found a better work around.
Hard to tell without being able to produce your plot but does using scales = "free_x" in your facet wrap call solve the problem?
For example:
facet_wrap(~ Site, ncol=1,nrow=4, scales = "free_x")
Might be late to the party, but my solution for this is replacing ggplot2::facet_wrap by lemon::facet_rep_wrap.

Resources