Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fd0fcf

Browse files
committed
fixed position jitterdodge example
1 parent e08d05d commit 6fd0fcf

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

_posts/ggplot2/2019-08-06-geom_jitter.Rmd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,13 @@ chart_link
185185
### Position Jitterdodge
186186
Up to this point, we've subdivided points by making one category the x-axis, and facetting by the other. Another way is to make one category the x-axis, then use "position = dodge" so that the points are distinct rather than overlapping. Since we want points to be jittered and dodged, we can use geom\_point with position\_jitterdodge().
187187

188+
Make sure to specify the "group" variable: this graph specifies three potential grouping variables (cluster, region, cd_code), and position\_jitterdodge can't tell which two to use unless specified. Further, you can use the ggplotly() function to specify what shows up on the tooltip.
189+
188190
```{r, results='hide'}
189191
library(plotly)
190192
191-
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region)) +
193+
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region,
194+
district=cd_code, group=paste(cluster, region))) +
192195
geom_point(position=position_jitterdodge(), alpha=0.5) +
193196
geom_hline(yintercept=0) +
194197
theme(axis.text.x = element_text(angle = -30, hjust = 0.1),
@@ -197,7 +200,7 @@ p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region)) +
197200
x = "Density Index from CityLab",
198201
y = "Democratic Margin of Victory/Defeat") +
199202
theme(text = element_text(family = 'Fira Sans'))
200-
ggplotly(p)
203+
ggplotly(p, tooltip=c("district","y"))
201204
202205
# Create a shareable link to your chart
203206
# Set up API credentials: https://plot.ly/r/getting-started

_posts/ggplot2/2019-08-06-geom_jitter.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ packageVersion('plotly')
3636
```
3737

3838
```
39-
## [1] '4.9.0.9000'
39+
## [1] '4.8.0.9000'
4040
```
4141

4242
### Basic Jitter Plot
@@ -181,11 +181,14 @@ chart_link
181181
### Position Jitterdodge
182182
Up to this point, we've subdivided points by making one category the x-axis, and facetting by the other. Another way is to make one category the x-axis, then use "position = dodge" so that the points are distinct rather than overlapping. Since we want points to be jittered and dodged, we can use geom\_point with position\_jitterdodge().
183183

184+
Make sure to specify the "group" variable: this graph specifies three potential grouping variables (cluster, region, cd_code), and position\_jitterdodge can't tell which two to use unless specified. Further, you can use the ggplotly() function to specify what shows up on the tooltip.
185+
184186

185187
```r
186188
library(plotly)
187189

188-
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region)) +
190+
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region,
191+
district=cd_code, group=paste(cluster, region))) +
189192
geom_point(position=position_jitterdodge(), alpha=0.5) +
190193
geom_hline(yintercept=0) +
191194
theme(axis.text.x = element_text(angle = -30, hjust = 0.1),
@@ -194,7 +197,7 @@ p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region)) +
194197
x = "Density Index from CityLab",
195198
y = "Democratic Margin of Victory/Defeat") +
196199
theme(text = element_text(family = 'Fira Sans'))
197-
ggplotly(p)
200+
ggplotly(p, tooltip=c("district","y"))
198201

199202
# Create a shareable link to your chart
200203
# Set up API credentials: https://plot.ly/r/getting-started

0 commit comments

Comments
 (0)