Set color for segments in ggnet - r

I hvar the following data set:
structure(c(2L, 6L, 2L, 6L, 7L, 7L, 2L, 7L, 6L, 8L, 8L, 4L, 8L,
2L, 9L, 8L, 7L, 6L, 9L, 1L, 9L, 4L, 9L, 3L, 2L, 10L, 9L, 10L,
8L, 10L, 7L, 6L, 10L, 1L, 2L, 12L, 9L, 8L, 12L, 1L, 11L, 10L,
2L, 44L, 79L, 10L, 8L, 47L, 45L, 51L, 9L, 11L, 74L, 75L, 77L,
69L, 75L, 77L, 78L, 2L, 44L, 44L, 46L, 46L, 8L, 6L, 1L, 1L, 6L,
7L, 1L, 4L, 7L, 8L, 8L, 1L, 4L, 8L, 3L, 8L, 8L, 9L, 9L, 9L, 1L,
9L, 5L, 9L, 3L, 9L, 9L, 9L, 10L, 8L, 10L, 7L, 10L, 10L, 1L, 10L,
10L, 9L, 12L, 12L, 1L, 12L, 12L, 12L, 12L, 7L, 7L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 7L, 7L, 7L, 7L, 44L,
10L, 9L, 42L, 43L, 46L, 46L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, -1L, -1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L), .Dim = c(66L,
3L), .Dimnames = list(NULL, c("from", "to", "impact")))
The data set indicates, a connection between from and to with a positive (1) or negative (-1) impact.
I would like to plot a network graph with ggnet (or ggplot2) that plot this graph.
So far I have done the following steps:
library(network)
library(ggplot2)
library(ggnet)
library(grid)
net <- network(df2[,c(1,2)], directed = FALSE)
ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=F, segment.alpha = 0.5, color = "black") +
theme(legend.position = "none") +
theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))
This lead to this result:
I am wondering how to colorize the edges based on the impact in the dataset (1 = green and -1 = red). I am also wondering why there are so many unconnected nodes...
Can someone help me with this?
Thanks a lot.

First, I am using GGally::ggnet as this is available through CRAN. I believe this is equivalent to that on github.
library(network)
library(GGally)
library(ggplot2)
# dependencies
library(grid)
library(sna)
library(intergraph)
The reason you have many unconnected nodes in your network diagram is because the node names you have supplied are not an unbroken sequence of integers. For example, if you supply only an edge between nodes named 1 and 10, network() will assume the presence of eight unconnected nodes with names 2:9. For example,
netwk1 <- network(cbind(1,2), directed = F)
get.vertex.attribute(netwk1, attrname="vertex.names")
netwk2 <- network(cbind(1,10), directed = F)
get.vertex.attribute(netwk2, attrname="vertex.names")
So if you convert your node names to an unbroken sequence, you will lose all the unconnected nodes. So something like:
df2[,1:2]=as.numeric(as.factor(df2[,c(1,2)]))
net <- network(df2[,c(1,2)], directed = F)
ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=T, segment.alpha = 0.5, color = "black") +
theme(legend.position = "none") +
theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))
You can color the edges with the segment.color argument in ggnet() :
edge_color = ifelse(df2[,3]==-1, "red", "green")
ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, segment.color=edge_color, label.nodes=T, segment.alpha = 0.5, color = "black") +
theme(legend.position = "none") +
theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))

You should include node.group =type
ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=F, segment.alpha = 0.5, color = "black", node.group=type) +
theme(legend.position = "none")+
theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))

You could also construct the plot directly using the network package, which I think ggnet is using under the hood.
# Dominik's data structure
edgelist<-structure(c(2L, 6L, 2L, 6L, 7L, 7L, 2L, 7L, 6L, 8L, 8L, 4L, 8L,
2L, 9L, 8L, 7L, 6L, 9L, 1L, 9L, 4L, 9L, 3L, 2L, 10L, 9L, 10L,
8L, 10L, 7L, 6L, 10L, 1L, 2L, 12L, 9L, 8L, 12L, 1L, 11L, 10L,
2L, 44L, 79L, 10L, 8L, 47L, 45L, 51L, 9L, 11L, 74L, 75L, 77L,
69L, 75L, 77L, 78L, 2L, 44L, 44L, 46L, 46L, 8L, 6L, 1L, 1L, 6L,
7L, 1L, 4L, 7L, 8L, 8L, 1L, 4L, 8L, 3L, 8L, 8L, 9L, 9L, 9L, 1L,
9L, 5L, 9L, 3L, 9L, 9L, 9L, 10L, 8L, 10L, 7L, 10L, 10L, 1L, 10L,
10L, 9L, 12L, 12L, 1L, 12L, 12L, 12L, 12L, 7L, 7L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 7L, 7L, 7L, 7L, 44L,
10L, 9L, 42L, 43L, 46L, 46L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, -1L, -1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L), .Dim = c(66L,
3L), .Dimnames = list(NULL, c("from", "to", "impact")))
# construct a network object
net<-as.network.matrix(edgelist,matrix.type='edgelist',
ignore.eval=FALSE,names.eval='impact')
# plot it, using the impact edge attribute to control edge color
plot(net,edge.col=ifelse(net%e%'impact'==1,'green','red'))
Nate Pope's answer above about changing the range of the ids to remove isolates still applies. However, you can ask plot.network not to draw the isolates:
plot(net,edge.col=ifelse(net%e%'impact'==1,'green','red'),displayisolates=FALSE)

Related

Editing a Row Value in R

I have a data frame that looks like this
Pick Name Team Round Player Position Position..
1 1 Javi Texans 1 Patrick Mahomes QB QB1
2 2 Justin Chiefs 1 Russell Wilson QB QB2
3 3 Blake Titans 1 Lamar Jackson QB QB3
4 4 Connor Dolphins 1 Deshaun Watson QB QB4
5 5 Isaac Jaguars 1 Carson Wentz QB QB5
6 6 Fitz Rams 1 Dak Prescott QB QB6
with more rows of course and some of the rows in the Player, Position and Position... Column are empty because they haven't been drafted yet. Is there a way to just manually insert the names, pos, pos... of the newly drafted players.
I tried
Redraft[112, "Player"] <- "Calvin Ridley"; Redraft
Since the empty cells start on row 112, but it just came up as N/A
When I do that I also get an error message:
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "Calvin Ridley") :
invalid factor level, NA generated
and the data frame looks like
08 108 Jack Packers 4 TE3 Darren Waller TE
109 109 Justin Saints 4 LT6 Taylor Lewan LT
110 110 Sam Steelers 4 FS5 Kevin Byard FS
111 111 Jeremy Falcons 4 LB7 Isaiah Simmons LB
112 112 Will Bills 4 1 <NA>
113 113 Jeremy Colts 4 1
And heres the whole data frame:
structure(list(Pick = 1:384, Name = structure(c(12L, 14L, 1L,
2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L, 13L, 20L,
13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L, 4L, 18L,
15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L, 8L, 14L,
17L, 13L, 20L, 13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L, 6L, 8L, 5L,
7L, 2L, 1L, 14L, 12L, 12L, 14L, 1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L,
9L, 2L, 10L, 16L, 11L, 13L, 20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L,
5L, 19L, 7L, 1L, 6L, 4L, 18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L,
19L, 5L, 19L, 3L, 3L, 8L, 14L, 17L, 13L, 20L, 13L, 11L, 16L,
10L, 2L, 9L, 12L, 9L, 6L, 8L, 5L, 7L, 2L, 1L, 14L, 12L, 12L,
14L, 1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L,
13L, 20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L,
4L, 18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L,
8L, 14L, 17L, 13L, 20L, 13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L,
6L, 8L, 5L, 7L, 2L, 1L, 14L, 12L, 12L, 14L, 1L, 2L, 7L, 5L, 8L,
6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L, 13L, 20L, 13L, 17L, 14L,
8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L, 4L, 18L, 15L, 15L, 18L,
4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L, 8L, 14L, 17L, 13L, 20L,
13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L, 6L, 8L, 5L, 7L, 2L, 1L,
14L, 12L, 12L, 14L, 1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L,
10L, 16L, 11L, 13L, 20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L,
19L, 7L, 1L, 6L, 4L, 18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L,
5L, 19L, 3L, 3L, 8L, 14L, 17L, 13L, 20L, 13L, 11L, 16L, 10L,
2L, 9L, 12L, 9L, 6L, 8L, 5L, 7L, 2L, 1L, 14L, 12L, 12L, 14L,
1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L, 13L,
20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L, 4L,
18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L, 8L,
14L, 17L, 13L, 20L, 13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L, 6L,
8L, 5L, 7L, 2L, 1L, 14L, 12L), .Label = c("Blake", "Connor",
"Dakota", "FFB", "Fitz", "Haydon", "Isaac", "Jack", "Jackson",
"Jacob", "Jacob H", "Javi", "Jeremy", "Justin", "Nick", "Pete",
"Sam", "Simon", "Tucker", "Will"), class = "factor"), Team = structure(c(30L,
10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L,
11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L,
12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L,
25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L,
8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L, 30L, 30L, 10L, 31L,
13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L, 11L, 4L,
15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L, 12L, 16L,
22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L, 25L, 1L,
20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L, 8L, 3L, 6L,
18L, 24L, 17L, 13L, 31L, 10L, 30L, 30L, 10L, 31L, 13L, 17L, 24L,
18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L, 11L, 4L, 15L, 29L, 27L,
20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L, 12L, 16L, 22L, 2L, 2L,
22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L, 25L, 1L, 20L, 27L, 29L,
15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L, 8L, 3L, 6L, 18L, 24L, 17L,
13L, 31L, 10L, 30L, 30L, 10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L,
8L, 7L, 28L, 9L, 21L, 14L, 11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L,
5L, 23L, 26L, 32L, 19L, 12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L,
19L, 32L, 26L, 23L, 5L, 25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L,
14L, 21L, 9L, 28L, 7L, 8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L,
30L, 30L, 10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L,
9L, 21L, 14L, 11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L,
26L, 32L, 19L, 12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L,
26L, 23L, 5L, 25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L,
9L, 28L, 7L, 8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L, 30L, 30L,
10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L,
11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L,
12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L,
25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L,
8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L, 30L), .Label = c("49ers",
"Bears", "Bengals", "Bills", "Broncos", "Browns", "Buccaneers",
"Cardinals", "Chargers", "Chiefs", "Colts", "Cowboys", "Dolphins",
"Eagles", "Falcons", "Giants", "Jaguars", "Jets", "Lions", "Packers",
"Panthers", "Patriots", "Raiders", "Rams", "Ravens", "Redskins",
"Saints", "Seahawks", "Steelers", "Texans", "Titans", "Vikings"
), class = "factor"), Round = 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, 2L, 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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,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L),
Pos.. = structure(c(49L, 60L, 70L, 71L, 72L, 73L, 74L, 75L,
27L, 76L, 43L, 50L, 51L, 92L, 52L, 53L, 54L, 55L, 77L, 56L,
57L, 58L, 89L, 59L, 85L, 61L, 103L, 3L, 106L, 35L, 62L, 36L,
63L, 42L, 10L, 107L, 18L, 64L, 108L, 65L, 109L, 19L, 11L,
110L, 86L, 37L, 111L, 12L, 20L, 112L, 66L, 21L, 38L, 13L,
90L, 78L, 81L, 30L, 14L, 15L, 82L, 39L, 16L, 17L, 93L, 94L,
4L, 22L, 95L, 96L, 2L, 97L, 67L, 5L, 68L, 87L, 83L, 84L,
6L, 31L, 44L, 98L, 99L, 100L, 7L, 28L, 101L, 32L, 29L, 8L,
33L, 88L, 69L, 79L, 102L, 9L, 104L, 40L, 23L, 24L, 105L,
25L, 45L, 80L, 46L, 26L, 47L, 91L, 48L, 34L, 41L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1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E7", "DE8", "DE9", "DT1", "DT2", "DT3", "FS1", "FS2", "FS3",
"FS4", "FS5", "LB1", "LB2", "LB3", "LB4", "LB5", "LB6", "LB7",
"LG1", "LT1", "LT2", "LT3", "LT4", "LT5", "LT6", "QB1", "QB10",
"QB11", "QB12", "QB13", "QB14", "QB15", "QB16", "QB17", "QB18",
"QB19", "QB2", "QB20", "QB21", "QB22", "QB23", "QB24", "QB25",
"QB26", "QB27", "QB28", "QB3", "QB4", "QB5", "QB6", "QB7",
"QB8", "QB9", "RB1", "RB2", "RB3", "RB4", "RG1", "RT1", "RT2",
"RT3", "SS1", "SS2", "SS3", "SS4", "TE1", "TE2", "TE3", "WR1",
"WR10", "WR11", "WR12", "WR13", "WR14", "WR15", "WR16", "WR17",
"WR18", "WR19", "WR2", "WR20", "WR21", "WR3", "WR4", "WR5",
"WR6", "WR7", "WR8", "WR9"), class = "factor"), Player = structure(c(87L,
91L, 72L, 38L, 14L, 24L, 79L, 78L, 3L, 57L, 90L, 70L, 107L,
31L, 39L, 10L, 56L, 68L, 20L, 4L, 94L, 93L, 45L, 52L, 51L,
44L, 80L, 97L, 62L, 67L, 40L, 98L, 101L, 89L, 50L, 9L, 85L,
104L, 19L, 41L, 109L, 58L, 106L, 81L, 37L, 26L, 29L, 27L,
18L, 86L, 60L, 84L, 17L, 74L, 105L, 95L, 111L, 63L, 76L,
55L, 92L, 110L, 12L, 15L, 64L, 96L, 34L, 25L, 61L, 103L,
54L, 21L, 53L, 49L, 59L, 108L, 71L, 83L, 77L, 82L, 102L,
7L, 65L, 2L, 69L, 32L, 22L, 75L, 43L, 5L, 8L, 46L, 35L, 42L,
23L, 88L, 6L, 11L, 60L, 48L, 16L, 13L, 30L, 36L, 73L, 33L,
99L, 28L, 100L, 66L, 47L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 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ackson", "Allen Robinson",
"Amari Cooper", "Anthony Harris", "Antonio Brown", "Baker Mayfield",
"Bobby Wagner", "Byron Jones ", "Cameron Jordan", "Carson Wentz",
"Casey Hayward", "CeDee Lamb", "Chandler Jones", "Chase Young",
"Chris Godwin", "Christian McCaffrey", "Cooper Kupp", "Courtland Sutton",
"D.J. Moore", "Dak Prescott", "Danielle Hunter", "Darius Leonard",
"Darius Slay", "Darren Waller", "DaVante Adams", "David Bakhtiari",
"DeAndre Hopkins", "Deforest Buckner", "Demarcus Lawrence",
"Denzel Ward", "Derek Carr", "Derrick Henry", "Derwin James",
"Deshaun Watson", "Drew Brees", "Drew Lock", "Dwayne Haskins",
"Ezekiel Elliott", "Fletcher Cox", "Gardner Minshew", "George Kittle",
"Harrison Smith", "Isaiah Simmons", "J.J. Watt", "Jaire Alexander",
"Jalen Ramsey", "Jamal Adams", "Jared Goff", "Jarrett Stidham",
"Jason Kelce", "Jeffrey Okudah", "Jimmy Garappolo", "Joe Burrow",
"Joey Bosa", "Jordan Love", "Josh Allen", "Juju Smith-Schuster",
"Julio Jones", "Justin Simmons", "Keenan Allen", "Kenny Golladay",
"Kevin Byard", "Khalil Mack", "Kirk Cousins", "Kyle Fuller",
"Kyler Murray ", "La'el Collins", "Lamar Jackson ", "Laremy Tunsil",
"Marcus Peters", "Marcus Williams", "Marlon Humphrey", "Marshon Lattimore",
"Matt Ryan", "Matthew Stafford", "Michael Thomas", "Mike Evans",
"Minkah Fitzpatrick", "Mitchell Schwartz ", "Myles Garrett",
"Nick Bosa", "Odell Beckham Jr.", "Patrick Mahomes ", "Patrick Peterson",
"Quenton Nelson", "Ronnie Stanley", "Russell Wilson ", "Ryan Ramczyk",
"Ryan Tannehill", "Sam Darnold", "Saquon Barkley", "Stefon Diggs",
"Stephon Gilmore", "T.J. Watt", "Taylor Decker", "Taylor Lewan",
"Teddy Bridgewater", "Terron Armstead", "Terry McLaurin",
"Tom Brady", "Travis Kelce", "Tre White", "Tua Tagovailoa",
"Tyrann Mathieu", "Tyreek Hill", "Von Miller", "Zack Martin"
), class = "factor"), Position = structure(c(10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 5L, 10L, 9L, 10L, 10L, 16L, 10L,
10L, 10L, 10L, 11L, 10L, 10L, 10L, 15L, 10L, 14L, 10L, 16L,
3L, 16L, 7L, 10L, 7L, 10L, 8L, 3L, 16L, 4L, 10L, 16L, 10L,
16L, 4L, 3L, 16L, 14L, 7L, 16L, 3L, 4L, 16L, 10L, 4L, 7L,
3L, 15L, 11L, 12L, 6L, 3L, 3L, 13L, 7L, 3L, 3L, 16L, 16L,
3L, 4L, 16L, 16L, 2L, 16L, 10L, 3L, 10L, 14L, 13L, 13L, 3L,
6L, 9L, 16L, 16L, 16L, 3L, 5L, 16L, 6L, 5L, 3L, 6L, 14L,
10L, 11L, 16L, 3L, 16L, 7L, 4L, 4L, 16L, 4L, 9L, 11L, 9L,
4L, 9L, 15L, 9L, 6L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 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("", "C",
"CB", "DE", "DT", "FS", "LB", "LG", "LT", "QB", "RB", "RG",
"RT", "SS", "TE", "WR"), class = "factor")), row.names = c(NA,
384L), class = "data.frame")
You're dealing with a factor column. "Calvin Ridley" isn't yet a level of the factor. After adding it you can rename the cell.
class(Redraft$Player)
# [1] "factor"
levels(Redraft$Player) <- c(levels(Redraft$Player), "Calvin Ridley")
Redraft[112, "Player"] <- "Calvin Ridley"
Redraft[112, "Player"]
# [1] Calvin Ridley
# 112 Levels: A.J. Brown Aaron Donald Aaron Rodgers Adoree' Jackson Allen Robinson ... Calvin Ridley
jay.sf's answer is correct, of course, but I'd add my 2¢ since I think it's missing the point.
The reason you have factors instead of plain strings here in the first place, is kind of a historical accident with R being a statistical language. In practice, you rarely want to be dealing with factors in a dataframe of this kind. You probably want your player names to be plain-old strings.
Typically when you read a dataframe from a file, e.g. via read.csv, you have the option to pass the argument stringsAsFactors = TRUE, to ensure that strings are kept as strings rather than converted to factors. Some people (e.g. this guy) feel so strongly against this bizzare default behaviour, that they include a line in their .Rprofile to make importing data with stringsAsFactors=T as their default. (but this is dangerous for writing code that works the same across users with different .Rprofile initializations!)
If you already have the dataset, you can convert your factors to strings instead:
df[ , 'Player'] <- as.character( df[ , 'Player' ] )
You can now continue with your analysis without worrying about factors and their annoyances.
E.g. setting a new name is as simple as you'd expect:
df[112,'Player'] <- 'Calvin Ridley'

R diff lead difference between two columns?

Here is my dataframe:
structure(list(replicate = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L), press_id = c(1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), start_time = c(164429106370979,
164429411618825, 164429837271940, 164430399454285, 164429106370980,
164429411618826, 164429837271941, 164430399454286, 164429106370981,
164429411618827, 164429837271942, 164430399454287, 164429106370982,
164429411618828, 164429837271943, 164430399454288, 164429106370983,
164429411618829, 164429837271944, 164430399454289, 164429106370984,
164429411618830, 164429837271945, 164430399454290, 164429106370985,
164429411618831, 164429837271946, 164430399454291, 164429106370986,
164429411618832, 164429837271947, 164430399454292, 164429106370987,
164429411618833, 164429837271948, 164430399454293, 164429106370988,
164429411618834, 164429837271949, 164430399454294, 164429106370989,
164429411618835, 164429837271950, 164430399454295, 164429106370990,
164429411618836, 164429837271951, 164430399454296, 164429106370991,
164429411618837, 164429837271952, 164430399454297, 164429106370992,
164429411618838, 164429837271953, 164430399454298, 164429106370993,
164429411618839, 164429837271954, 164430399454299), end_time = c(164429182443825,
164429512525748, 164429903243170, 164430465927555, 164429182443826,
164429512525749, 164429903243171, 164430465927556, 164429182443827,
164429512525750, 164429903243172, 164430465927557, 164429182443828,
164429512525751, 164429903243173, 164430465927558, 164429182443829,
164429512525752, 164429903243174, 164430465927559, 164429182443830,
164429512525753, 164429903243175, 164430465927560, 164429182443831,
164429512525754, 164429903243176, 164430465927561, 164429182443832,
164429512525755, 164429903243177, 164430465927562, 164429182443833,
164429512525756, 164429903243178, 164430465927563, 164429182443834,
164429512525757, 164429903243179, 164430465927564, 164429182443835,
164429512525758, 164429903243180, 164430465927565, 164429182443836,
164429512525759, 164429903243181, 164430465927566, 164429182443837,
164429512525760, 164429903243182, 164430465927567, 164429182443838,
164429512525761, 164429903243183, 164430465927568, 164429182443839,
164429512525762, 164429903243184, 164430465927569)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -60L), vars = c("replicate",
"press_id"), drop = TRUE, indices = list(0L, 1L, 2L, 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), group_sizes = 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), biggest_group_size = 1L, labels = structure(list(
replicate = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L), press_id = c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L)), class = "data.frame", row.names = c(NA,
-60L), vars = c("replicate", "press_id"), drop = TRUE, indices = list(
0L, 1L, 2L, 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), group_sizes = 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), biggest_group_size = 1L, labels = structure(list(
replicate = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L), press_id = c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L)), class = "data.frame", row.names = c(NA,
-60L), vars = c("replicate", "press_id"), drop = TRUE, .Names = c("replicate",
"press_id")), .Names = c("replicate", "press_id")), .Names = c("replicate",
"press_id", "start_time", "end_time"))
I want to get the inter press_id time diff for example:
replicate press_id start_time end_time time_diff
1 1 1.644291e+14 1.644292e+14 0 (it's a first row)
1 2 1.644294e+14 1.644295e+14 1.644294e+14 - 1.644292e+14
1 3 1.644298e+14 1.644299e+14 1.644298e+14 - 1.644295e+14
1 4 1.644304e+14 1.644305e+14 .....
2 1 1.644291e+14 1.644292e+14
2 2 1.644294e+14 1.644295e+14
2 3 1.644298e+14 1.644299e+14
2 4 1.644304e+14 1.644305e+14
I am trying to do this using mutate, lag, lead and diff but without any luck. I have grouped, and ungrouped the dataset, nothing helped me.
df %>%
group_by(replicate) %>%
mutate(d = ifelse(row_number() == 1, 0, lead(start_time) - end_time))
df %>%
group_by(replicate) %>%
mutate(d = start_time - lag(end_time))
And if you want zeroes except NAs for the first row of each unique value in the replicate column, you could do:
df %>%
group_by(replicate) %>%
mutate(d = start_time - lag(end_time),
d = ifelse(is.na(d), 0, d))
Or just:
df %>%
group_by(replicate) %>%
mutate(d = ifelse(row_number() == 1, 0, start_time - lag(end_time)))

Facets: organising their order and organising the levels within facets

I would like to please organise the following plots so that facets are printed out from most to least busy (i.e. Hemiptera, Coleoptera, Hymenoptera, Siphonaptera, Lepidoptera, etc.)
I would also like to order the levels within each facet like in Coleoptera. I realise that the X-labels will change order too so I need each facet to print out its own X-label according the level order.
I have already read many threads and that's how I was able to organise Coleoptera. But now I want it to be more tidy.
This is the data (let me know if this format is ok, if not I can try another way):
structure(list(Order = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("Coleoptera",
"Dermaptera", "Dictyoptera", "Diptera", "Hemiptera", "Hymenoptera",
"Lepidoptera", "Phthiraptera", "Psocoptera", "Siphonaptera",
"Thysanoptera"), class = "factor"), Nrange = structure(c(1L,
3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 14L, 14L, 1L, 10L, 1L,
3L, 4L, 6L, 7L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L, 10L, 11L,
12L, 14L, 15L, NA, 1L, 2L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L,
13L, 14L, 4L, 10L, 11L, 12L, 14L, 1L, 4L, 10L, 11L, 12L, 13L,
14L, 1L, 5L, 10L, 1L, 4L, 6L, 7L, 10L, 11L, 12L, 14L), .Label = c("Africa",
"Africa, Asia", "Americas", "Asia", "Asia-Temp", "Asia-Trop",
"Australasia", "C&S America", "Cosmopolitan", "Cryptogenic",
"N America", "S America", "Trop", "Trop, SubTrop", "Unknown"), class = "factor"),
Records = c(16L, 1L, 9L, 7L, 11L, 17L, 1L, 15L, 8L, 8L, 5L,
1L, 2L, 1L, 5L, 1L, 1L, 1L, 1L, 9L, 9L, 2L, 1L, 4L, 11L,
10L, 30L, 15L, 9L, 2L, 2L, 2L, 34L, 11L, 21L, 1L, 21L, 16L,
8L, 1L, 14L, 3L, 5L, 25L, 4L, 2L, 1L, 1L, 8L, 1L, 10L, 1L,
2L, 1L, 1L, 8L, 5L, 2L, 1L, 2L, 2L, 9L, 1L, 2L, 1L, 3L, 1L,
12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L,
3L, 2L)), .Names = c("Order", "Nrange", "Records"), row.names = c(NA,
-83L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Order", drop = TRUE)
This is the reordering that I guess is affecting only Coleoptera.
xy<-x%>%
mutate(Nrange=reorder(Nrange,-Records,sum))
This is the plot:
to_plot<-xy %>%
filter(!is.na(Nrange))
ggplot(to_plot,aes(x=Nrange,y=Records,fill=Nrange))+
geom_col()+
theme(axis.text.x = element_text(angle=90, vjust=0.7), legend.position = "none") +
facet_wrap(~Order,ncol=3)+
labs(title="Insects recorded as alien-invasive to mainland Spain",
subtitle="Native ranges vs number of records",
caption="Data source: DAISIE (http://www.europe-aliens.org/)")
And this is the plot:
enter image description here
Assuming you're using the tidyverse (based on your code):
library(tidyverse)
xy <- x %>%
ungroup() %>%
mutate(
Order = fct_reorder(Order, Records, sum, .desc = TRUE)
)
xy %>%
filter(!is.na(Nrange)) %>%
ggplot() +
aes(x = Nrange, y = Records, fill = Nrange) +
geom_col() +
facet_wrap(~Order, ncol = 3)
fct_reorder comes from the forcats package, which I believe is now a part of the tidyverse.
Or, using base R, something like this:
xy <- x
record_sums <- tapply(xy$Records, xy$Order, sum)
levels(xy$Order) <- levels(xy$Order)[order(record_sums, decreasing = TRUE)]

plot group and category means with group_by

I am new to R and trying to figure out a way to plot means for individual samples as well as group means with ggplot.
I am following this articles on R-bloggers (last paragraph):
https://www.r-bloggers.com/plotting-individual-observations-and-group-means-with-ggplot2/
This is my code:
gd <- meanplot1 %>%
group_by(treatment, value) %>%
summarise(measurement = mean(measurement))
ggplot(meanplot1, aes(x=value, y=measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
geom_line(data=gd, size=3, alpha=0.9) +
theme_bw()
Whilst the sample means are being shown, the group means aren´t. I get the error
geom_path: Each group consists of only one observation. Do you need
to adjust the group aesthetic?
Upon adding group=1, I get a weirdly mixed category mean, but not what I am looking for..
I scrolled through a lot of articles already, but couldnt find an answer - I would be so happy if somebody could help me out here!! :)
My data (meanplot1) is formatted like this:
treatment sample value measurement
1 control, control 1, initial, 20,
2 control, control 1, 26, NA,
3 control, control 1, 26', 28,
12 control, control 2, initial, 22,
13 control control 2, 26, NA,
14 control control 2, 26', 36,
15 control control 2, 28, 45,
67 stressed, stress 1, initial, 37,
68 stressed, stress 1, 26, NA,
69 stressed, stress 1, 26', 17,
78 stressed, stress 2, initial, 36,
79 stressed, stress 2, 26, NA,
80 stressed, stress 2, 26', 25,
I am hoping to see 6 lines, one mean for stress 1, stress 2, control 1 and control 2, and one mean for all treatment=control, and one for all treatment=stressed
output dput(gd):
structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("control", "stressed"), class = "factor"), value = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L), .Label = c("26", "26'", "28", "28'",
"30", "30'", "32", "32'", "34", "34'", "initial"), class = "factor"),
measurement = c(NA, 32.3333333333333, 39.5, 30.3333333333333,
31.8333333333333, 31.8333333333333, NA, 36, 34.6666666666667,
36, 24.6666666666667, NA, 25.3333333333333, 33.3333333333333,
32, 50.1666666666667, 39.1666666666667, NA, 33.5, 24.3333333333333,
27.3333333333333, 36)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -22L), vars = list(treatment), drop = TRUE, .Names = c("treatment",
"value", "measurement"))
output dput(meanplot1):
structure(list(treatment = 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, 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), .Label = c("control",
"stressed"), class = "factor"), sample = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("control 1",
"control 2", "control 3", "control 4", "control 5", "control 6",
"stress 1", "stress 2", "stress 3", "stress 4", "stress 5", "stress 6"
), class = "factor"), value = structure(c(11L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("26", "26'",
"28", "28'", "30", "30'", "32", "32'", "34", "34'", "initial"
), class = "factor"), measurement = c(20L, NA, 28L, 18L, 17L,
19L, 34L, NA, 23L, 29L, 27L, 22L, NA, 36L, 45L, 31L, 40L, 44L,
NA, 49L, 40L, 39L, 32L, NA, 35L, 57L, 30L, 37L, 29L, NA, 44L,
37L, 46L, 20L, NA, 39L, 27L, 30L, 40L, 25L, NA, 29L, 50L, 30L,
26L, NA, 28L, 45L, 47L, 27L, 35L, NA, 24L, 22L, 35L, 28L, NA,
28L, 45L, 27L, 28L, 24L, NA, 47L, 30L, 39L, 37L, NA, 17L, 29L,
29L, 31L, 29L, NA, 37L, 21L, 27L, 36L, NA, 25L, 41L, 51L, 66L,
50L, NA, 33L, 25L, 22L, 36L, NA, 33L, 45L, 26L, 72L, 59L, NA,
33L, 26L, 25L, 33L, NA, 21L, 33L, 25L, 29L, 21L, NA, 26L, 20L,
16L, 22L, NA, 30L, 27L, 28L, 57L, 41L, NA, 28L, 23L, 17L, 52L,
NA, 26L, 25L, 33L, 46L, 35L, NA, 44L, 31L, 57L)), .Names = c("treatment",
"sample", "value", "measurement"), class = "data.frame", row.names = c(NA,
-132L))
I suppose you are aiming to plot the treatment means.
By default, since you are using a categorical x-axis, the grouping is set to the interaction between x and color. You only want to group by treatment, however. So we'll add the correct grouping to the call.
ggplot(meanplot1, aes(x = value, y = measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
geom_line(aes(group = treatment), gd, size=3, alpha=0.9) +
theme_bw()
Also note that
ggplot(meanplot1, aes(x=value, y=measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
stat_summary(aes(group = treatment), fun.y = mean, geom = 'line', size=3, alpha=0.9) +
theme_bw()
Gives the same plot, without the interruption.

Adding points only to selected levels of a boxplot

I have the following data frame:
> glimpse(pd)
Observations: 340
Variables: 4
$ gene_id <fctr> T03F1.6, T01B11.2, F40D4.13, F38B6.4, F10F2....
$ inter <fctr> K9me3, K9me3, K9me3, K9me3, K9me3, K9me3, K9...
$ genotype <fctr> 641, 641, 641, 641, 641, 641, 641, 641, 641,...
$ value <dbl> 0.88733425, -0.47734512, 0.16116906, -0.40425...
and plotting code:
p4 <- ggplot(pd) +
geom_point(aes(x=inter,
y=value,
col=inter, shape=genotype, alpha=genotype), size = 3) +
scale_color_manual(values = cc, name = ' ') +
scale_alpha_discrete(range=c(0.6,1)) +
myggplot.y0 +
theme(legend.position="bottom") +
myggplot.blankXtext +
facet_wrap(~gene_id, ncol=2) +
guides(color=guide_legend(title=''))
p4
I'd like to summarize the round shaped points by a boxplot (at each dodging level), but keep the triangle shaped points (which only appear in the last two 'levels') as separate 'points' overlayed on the boxplot.
Does anyone know how to do this?
Here is the data to reproduce the data.frame:
> dput(pd)
structure(list(gene_id = structure(c(8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 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, 16L, 16L, 16L, 16L, 16L, 16L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), .Label = c("K27me0",
"K27me1", "K27me2", "K27me3", "K36me0", "K36me1", "K36me2", "K36me3",
"K4me0", "K4me1", "K4me2", "K4me3", "K9me0", "K9me1", "K9me2",
"K9me3"), class = "factor"), genotype = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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("N2", "641"), class = "factor"), value = c(0.887334249026434,
-0.47734511528238, 0.161169059129204, -0.40425962740708, -0.448772664830352,
-2.54043183911831, 0.147633437904588, -0.331999887972573, 0.168821284679471,
0.627281852456115, 0.0785458811781217, -0.635417116680447, 1.95039267129571,
-0.461389508271182, -0.332495133573183, -1.07629215381253, -0.715262085650234,
0.380580191285256, -0.384548303669202, -0.492327117463121, -0.0635811095948338,
-0.137169496659571, 0.869104975820394, -0.124274416739264, -0.00837899314070789,
-0.531344335277651, -0.526039923426319, 0.157981596945951, -0.0613955127076382,
-0.389835314108788, 2.69392711479386, 0.126789590130756, 0.467843320254945,
0.119956248487539, -0.932684036187982, 1.06163513421864, -1.15599025769053,
0.501998795474814, 0.547724207589878, 0.208660744829548, 2.39652396988934,
-0.113536659142323, 0.617602997781084, -0.0243661090708898, -0.931766154859557,
0.84708053705325, -0.302694166168958, 0.242295516669482, 0.362144368539548,
0.143166134234178, 2.10369156882084, 0.460380422671629, -0.0118030947996877,
-1.30190938551855, -0.944990285797225, 1.78280710295825, -0.634879160361434,
0.633411617712061, -0.323643001568312, -0.352447999186893, 0.27261554500361,
-0.184793596205417, -0.785654613201974, -0.380434850649155, -0.116175416888516,
0.391864521424546, 0.288960168023483, -0.0808022493276015, -0.624387637961664,
-0.652297254014572, 1.75147794216705, -0.439685382191905, 0.150488781634707,
-0.0336437864682964, -0.802955390112682, 1.06183416852385, -0.923263618312843,
0.468046260016009, 0.286851381130517, 0.17305984039931, 1.75158852119529,
-0.592564500094055, 0.248498177746989, -0.222638458602893, -0.720265703696414,
0.72750611824884, -0.336818721832683, 0.511031230869627, 0.0430587972307137,
0.0290598947165659, 0.886443959972121, -0.303364064568797, -0.277915654881935,
-0.462931588140646, -0.512912057394122, 0.130398107823421, -0.410559505055534,
-0.543074481188406, 0.223303098899384, -0.35597446101497, 0.584846557204577,
-0.328552338525071, 0.39166741782988, -0.419624587301293, -0.417042458658473,
-0.0500348090171157, 0.440292934053895, -0.0389439842100643,
-0.192935439273113, -0.195280031711113, -0.330623439443579, 0.406193213631403,
1.60465863614407, 0.488558950927488, 0.306422606850451, 0.0259842742803569,
0.464781609520434, 0.382521939171359, -0.697256999098308, 0.74469109948364,
0.0564480245933332, 0.451572695093123, 1.65543142016647, 0.471407535866445,
0.287994086588806, -0.589245829633368, 0.369581847988354, 0.597812561310243,
-0.607302119427235, 0.9696199804852, 0.153586315079381, -0.619187466367118,
0.656675901432987, -0.871392537978112, -0.311214917388622, -0.923682063590928,
-0.323604082561573, -0.693266286639908, 0.0290631195420703, -0.291795800840029,
-0.0916009854020086, -0.529683872226366, 0.341292466699543, -0.964841093192905,
-0.349467794977152, -0.502029626725142, -0.254774675707687, -1.20345690995063,
0.220507237168474, -0.954023439798044, 0.321770602015399, -0.726063502895515,
0.28288258484054, -1.55107383696037, -0.366037696773723, -0.587999636824452,
-1.35916629238201, -1.42792505502871, 1.05596729613068, -1.4153269555524,
-0.0959965268426171, -0.581692078188237, 0.0216148704828045,
-0.967560147159656, -0.406889554155961, -0.29013462836955, -0.272629044721009,
-1.74574575300093, 0.409694897073192, -0.945248012942512, 0.488667747235731,
-0.130769790765262, -0.245054023224077, -1.22641996252332, -0.42550379283728,
-0.330755471294788, -1.49812536880092, -0.687381177317297, 0.506910130730609,
-0.769620490644054, 0.0531265417354891, -0.665086454118599, 0.136193457296536,
-0.62216089856077, -0.367848687570944, -0.164342714173533, -0.382590305354972,
-0.874808163574018, 0.493411933475686, -0.37887729896755, -0.574238871100946,
0.127741068021188, 3.86039295680539, 0.406874973642696, 0.018180549517699,
-0.73164426806948, 0.10916295082713, 0.489171565128614, -0.63809359131097,
0.012317883603111, -0.182079038293229, 0.0227150325950261, 3.68710015302125,
0.455122847588301, 0.0235041737883517, -0.024393097564638, 0.41345288614306,
0.969811866964937, -0.672585115335576, 0.331895771644743, -0.285694918186591,
-0.711647452923045, 1.38434450822266, -0.523103532001491, -0.0893223046296763,
-1.01365895485872, 0.0657806542743673, -1.39534058124217, -0.923222605651799,
-1.39887741940789, -0.0952580333454192, -0.546574104718356, 1.46477855989728,
-0.460227934172416, -0.240045678851763, -0.0111436043830646,
0.0627986215438661, -0.251396685372123, -0.934093688281673, -1.05820277806975,
0.556102701675237, -0.506831613088146, 0.134996075420438, -0.304509562226102,
-0.530606844852739, -0.576258795363492, 0.558240796235196, 0.00141004667762168,
-0.219444435445798, -0.537347914917884, 0.923884368998904, -0.425823765099706,
0.402984847066973, -0.0138900208495514, -0.466549078530587, -0.0299175806841818,
-0.198688733963097, 0.419444078428615, -0.416005826002221, -0.0406133025559159,
-0.711562592297027, -0.00565350152856414, 4.54871285677085, 0.118779124267025,
0.0139761953422664, -0.664583716722071, -0.743919010180514, 0.706218216909635,
-0.683249460883233, 0.0605499462979795, -0.158429916510739, -0.222706230740269,
4.71119851589582, 0.25500719760156, -0.0752734072712284, -0.795844146522265,
0.178040897179368, 0.895173312761832, -1.03093515891618, -0.588056522862408,
-0.281825764719025, -0.056228940433579, -0.313824928780573, -0.0917469172753451,
-0.122912334871057, 0.712942661491444, -0.0416925666113199, -0.103626376180252,
-0.123906792244526, -0.338488229262714, -0.285202171459416, -0.012465219280215,
-0.140276363711298, -0.116007895520325, -0.111935190660002, 0.225067028104865,
0.435777323306682, -0.107111364797968, -0.0547435542980068, -0.245852927399465,
-0.646765570905925, -0.574523578850278, 1.23916851484567, -0.756916946637266,
-0.442286710512833, -0.653558388340836, -0.104749483441292, -0.0656026501027611,
-0.611101876494586, -1.24462228141138, -1.08065682436839, -0.11380843108228,
4.76147315049708, 0.0164152091429139, -0.0182056738105381, -0.385790482896067,
1.20308626895301, -0.241046175728772, 0.0412353010350772, -1.37886106242171,
-0.652945846253202, 0.0284072616942028, 0.780335178644036, -0.644783275438296,
-0.319045839781824, 1.57554487548949, -0.428706034693799, -0.350808314840347,
-0.24066856927377, -1.04530746723789, -0.240526163518468, -0.437546091131759,
3.36251366149142, -0.101287227327982, -0.149730531058178, -2.05718920626846,
0.316516294262123, 0.798192960479417, -1.0788450246926, -0.0806608802523838,
-0.670068360692003, -0.419448747866297, 6.36279926782848, -0.0378872162137345,
-0.692973015966929, -1.31677539848766, -0.576391545143803, 1.42209640226752,
-1.13065080356991, 0.26979963818752)), .Names = c("gene_id",
"inter", "genotype", "value"), class = "data.frame", row.names = c(NA,
-340L))
Use subsets of the data for the separate geoms:
ggplot(mapping = aes(x=inter, y=value)) +
geom_boxplot(data = subset(pd, genotype == "N2")) +
geom_point(data = subset(pd, genotype == "641"), aes(col=inter), size = 3) +
theme_bw() +
theme(legend.position="bottom",
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
facet_wrap(~gene_id, ncol=2) +
guides(color=guide_legend(title=''))
Note that a bunch of you plotting code isn't actually reproducible since it relies on objects such as myggplot.y0.

Resources