I want to plot all the x values on the x-axis, but only some of them are shown. Usually, I would have just use the as.factor()
command, but that one solves that problem but creates another one.
This is the code I use to plot:
values_map <- aes(
x = as.factor(type),
y = type_average
)
plot <- (ggplot(data=data_rpi, mapping=values_map)
geom_boxplot()
geom_point(data = data_rpi_mean, mapping = aes(x=as.factor(type), y=mean), color="red")
stat_smooth(method='lm', data = data_rpi_mean,
formula = y~poly(x,2), se=0, inherit.aes = FALSE,
aes(x=as.factor(type), y=mean))
labs(x = "x", y = "y")
)
plot
When I wrap up the type
parameter in a as.factor()
I do get all the wanted x ticks labels, but it does not plot the line any more. This is the picture with as.factor()
.
And this is how it looks when type
is not wrapped up in as.factor
.
I want to get the plot with both the line and all the labels on the x axis. Can you help me solve this problem?
The data files that I am using:
data_rpi
type,type_average
10,1.3519068364423203
10,1.0457766739360386
10,0.9913666987245522
10,0.8697531161071042
10,1.0558935287880695
10,1.088925726783231
10,0.8449061432373881
10,1.3001207478542471
10,1.121569136540713
10,0.9039893920532069
10,1.2013104898710774
10,0.9994338078788085
10,1.2190976293450526
10,0.8507231289299426
10,0.9823071321805434
10,1.1107502514066927
10,1.4835056148699683
10,0.8202130303647834
10,1.3540206441863551
10,1.0655716570887397
10,1.2312429669800213
10,1.015428953375069
10,1.0429651600909162
10,0.9270238572171099
10,1.1888699292516876
10,1.0986322185027082
10,0.707061312467999
10,1.1324027248615467
10,1.599119344884365
10,0.8870225481785545
10,0.9363124880095173
10,0.8043274870615695
10,0.9454452538911617
10,0.7762770915817487
10,1.041436784671713
10,1.1111834346818346
10,1.0388282009682044
10,0.8257604158413714
10,0.9674546100002424
10,1.016330929075771
15,1.111063986541283
15,1.1538527745012854
15,1.09857944131545
15,1.0134786569946073
15,0.8430615486890098
15,1.1694596875976875
15,1.1077471144215552
15,1.1014795573027336
15,0.9618605435729153
15,1.1126948103728944
15,0.9881304427287989
15,1.1919584128964864
15,1.2064571464541074
15,1.0007397798719113
15,1.1302386036211534
15,1.2040316082725675
15,1.2973945592025338
15,1.3631053906773487
15,0.9380412124207114
15,1.217197509617987
15,1.2068358855161905
15,1.107037369834412
15,0.9554552183455955
15,1.5505175130765703
15,0.9860335076507956
15,1.0848014284230283
15,0.976196203986281
15,1.2150210914187451
15,0.7515618465919158
15,1.0978717550328856
15,0.9647118898019622
15,1.2257804218483614
15,1.42314621600682
15,1.349544759709096
15,1.0752353642432513
15,1.1378474496288145
15,1.1731495721055532
15,1.1711263883075786
15,1.0343509999993272
15,1.1251658936735902
20,1.014592832397828
20,1.2258208471272458
20,0.9668310488498175
20,1.2412346556145857
20,1.185132630487507
20,1.1502553270303038
20,1.1029014422907713
20,1.3518669361005435
20,0.8680404252979512
20,1.2834946403978864
20,1.0661751210088743
20,1.2334000143938564
20,1.136718210579673
20,1.1920600870156375
20,1.2081368183806656
20,1.0118616489050019
20,1.1002653886978866
20,1.0972596026383146
20,1.0503307477291464
20,1.1110394991558628
20,1.091261061622069
20,1.206993973797632
20,1.0801237515256594
20,1.285586532730707
20,1.156421164746869
20,1.1281579673444537
20,1.2724090095988472
20,1.1331181569429032
20,1.2018608667572939
20,1.2389894123177516
20,1.208115954460383
20,1.2133311552059864
20,1.2897809434160847
20,1.4468809894605088
20,1.3014678684115144
20,1.1972402083552973
20,1.2460488717366913
20,1.517022934706687
20,1.408392742441523
20,1.4154250097979397
25,1.288570223826207
25,1.3000927989685702
25,1.1883346036017501
25,1.3105422486501364
25,1.4947035495467014
25,1.136269730206962
25,1.5286804591649341
25,1.5909965136185167
25,1.4380829945400522
25,1.2813062259586099
25,1.2672824688725255
25,1.2332297906134286
25,1.4761417189072392
25,1.278446329071691
25,1.5152914050011725
25,1.4397050887699994
25,1.2760290655555093
25,1.2899177179222416
25,1.2796147384257055
25,1.3095982745092765
25,1.4033074787903934
25,1.3235904042466293
25,1.319565253733844
25,1.433233635739164
25,1.1802582818167542
25,1.4788358854284458
25,1.3557638447148919
25,1.0976903084438763
25,1.2883532406651956
25,1.2748098428023962
25,1.4123457958622643
25,1.173288734190785
25,1.37081999605011
25,1.0008214474280321
25,1.3455949480447553
25,1.3546270037432668
25,1.3248075801031773
25,1.2057401779870869
25,1.1863460544034004
25,1.152499639308624
5,1.0575578471446856
5,0.8875217580697354
5,0.8076295798252193
5,0.8623057504838103
5,0.6516486395930943
5,0.6329410128341197
5,1.068471137054714
5,0.49964551672826385
5,0.544275194692331
5,0.5985422820395505
5,0.9569046823588474
5,0.7044962967684971
5,0.8377111042568894
5,0.9459527430846872
5,0.2629158202640962
5,0.8309368716512116
5,0.8494435103787943
5,0.9048414178122917
5,0.5414225362790117
5,1.2046043730011533
5,0.6596904282612961
5,0.856919502558009
5,0.795988494760103
5,1.0293962754338772
5,0.5435125760580548
5,0.452017243315582
5,0.9483986788144896
5,1.1684291269702585
5,0.7157929569085586
5,0.6830788789547988
5,1.272413923125478
5,0.8755585901925067
5,0.6999238331696982
5,0.9021532291313292
5,0.6485783416146771
5,0.7544413391331983
5,1.318788735178336
5,0.9693411455900005
5,0.7272624712553908
5,1.379662209509593
and data_rpi_mean
type,mean
10,1.0488566774545487
15,1.120549089056845
20,1.190901162486904
25,1.3151283874808581
5,0.826277901356406
CodePudding user response:
Two things here:
- Add
group = type
in youraes()
so that the box plot can be displayed individually - Use
scale_x_continuous(breaks = unique(data_rpi$type))
to include all unique labels on the x-axis (settinglabels
alone might not help)
library(tidyverse)
values_map <- aes(
x = type,
y = type_average,
group = type
)
ggplot(data=data_rpi, mapping=values_map)
geom_boxplot()
geom_point(data = data_rpi_mean, mapping = aes(x=type, y=mean), color="red")
stat_smooth(method='lm', data = data_rpi_mean,
formula = y~poly(x,2), se=0, inherit.aes = FALSE,
aes(x=type, y=mean)) labs(x = "x", y = "y")
scale_x_continuous(breaks = unique(data_rpi$type))
CodePudding user response:
Do not use factors if the underlying data is not a categorical variable. This should do the job:
p <- p scale_x_continous(labels = seq(5, 25, by = 5))