0

I am trying to make an interactive plot with the plotly package. The legend needs to be next to the image and have a certain width. I now have the issue that the default appears to be that the legend get wider as the plot gets wider, but this is undesirable. To specify a fixed length in pixels there is the entrywidth argument but I'm not getting it to work. See data and images below:

structure(list(Category = c("five", "four", "one", "seven", "six", 
"three", "two", "five", "four", "one", "seven", "six", "three", 
"two", "five", "four", "one", "seven", "six", "three", "two", 
"five", "four", "one", "seven", "six", "three", "two", "five", 
"four", "one", "seven", "six", "three", "two", "five", "four", 
"one", "seven", "six", "three", "two", "five", "four", "one", 
"seven", "six", "three", "two", "five", "four", "one", "seven", 
"six", "three", "two", "five", "four", "one", "seven", "six", 
"three", "two", "five", "four", "one", "seven", "six", "three", 
"two", "five", "four", "one", "seven", "six", "three", "two", 
"five", "four", "one", "seven", "six", "three", "two", "five", 
"four", "one", "seven", "six", "three", "two", "five", "four", 
"one", "seven", "six", "three", "two", "five", "four", "one", 
"seven", "six", "three", "two", "five", "four", "one", "seven", 
"six", "three", "two", "five", "four", "one", "seven", "six", 
"three", "two", "five", "four", "one", "seven", "six", "three", 
"two", "five", "four", "one", "seven", "six", "three", "two", 
"five", "four", "one", "seven", "six", "three", "two", "five", 
"four", "one", "seven", "six", "three", "two", "five", "four", 
"one", "seven", "six", "three", "two", "five", "four", "one", 
"seven", "six", "three", "two", "five", "four", "one", "seven", 
"six", "three", "two", "five", "four", "one", "seven", "six", 
"three", "two", "five", "four", "one", "seven", "six", "three", 
"two", "five", "four", "one", "seven", "six", "three", "two", 
"five", "four", "one", "seven", "six", "three", "two", "five", 
"four", "one", "seven", "six", "three", "two", "five", "four", 
"one", "seven", "six", "three", "two", "five", "four", "one", 
"seven", "six", "three", "two", "five", "four", "one", "seven", 
"six", "three", "two", "five", "four", "one", "seven", "six", 
"three", "two", "five", "four", "one", "seven", "six", "three", 
"two"), Order = c("other", "other", "other", "other", "other", 
"other", "other", "o.Anaerolineales", "o.Anaerolineales", "o.Anaerolineales", 
"o.Anaerolineales", "o.Anaerolineales", "o.Anaerolineales", "o.Anaerolineales", 
"o.Bacteriovoracales", "o.Bacteriovoracales", "o.Bacteriovoracales", 
"o.Bacteriovoracales", "o.Bacteriovoracales", "o.Bacteriovoracales", 
"o.Bacteriovoracales", "o.Bacteroidales", "o.Bacteroidales", 
"o.Bacteroidales", "o.Bacteroidales", "o.Bacteroidales", "o.Bacteroidales", 
"o.Bacteroidales", "o.Bdellovibrionales", "o.Bdellovibrionales", 
"o.Bdellovibrionales", "o.Bdellovibrionales", "o.Bdellovibrionales", 
"o.Bdellovibrionales", "o.Bdellovibrionales", "o.Burkholderiales", 
"o.Burkholderiales", "o.Burkholderiales", "o.Burkholderiales", 
"o.Burkholderiales", "o.Burkholderiales", "o.Burkholderiales", 
"o.Campylobacterales", "o.Campylobacterales", "o.Campylobacterales", 
"o.Campylobacterales", "o.Campylobacterales", "o.Campylobacterales", 
"o.Campylobacterales", "other", "other", "other", "other", "other", 
"other", "other", "o.Desulfobacterales", "o.Desulfobacterales", 
"o.Desulfobacterales", "o.Desulfobacterales", "o.Desulfobacterales", 
"o.Desulfobacterales", "o.Desulfobacterales", "o.Desulfuromonadales", 
"o.Desulfuromonadales", "o.Desulfuromonadales", "o.Desulfuromonadales", 
"o.Desulfuromonadales", "o.Desulfuromonadales", "o.Desulfuromonadales", 
"o.Erysipelotrichales", "o.Erysipelotrichales", "o.Erysipelotrichales", 
"o.Erysipelotrichales", "o.Erysipelotrichales", "o.Erysipelotrichales", 
"o.Erysipelotrichales", "o.Flavobacteriales", "o.Flavobacteriales", 
"o.Flavobacteriales", "o.Flavobacteriales", "o.Flavobacteriales", 
"o.Flavobacteriales", "o.Flavobacteriales", "o.Immundisolibacterales", 
"o.Immundisolibacterales", "o.Immundisolibacterales", "o.Immundisolibacterales", 
"o.Immundisolibacterales", "o.Immundisolibacterales", "o.Immundisolibacterales", 
"other", "other", "other", "other", "other", "other", "other", 
"o.Methanomicrobia", "o.Methanomicrobia", "o.Methanomicrobia", 
"o.Methanomicrobia", "o.Methanomicrobia", "o.Methanomicrobia", 
"o.Methanomicrobia", "o.Pseudomonadales", "o.Pseudomonadales", 
"o.Pseudomonadales", "o.Pseudomonadales", "o.Pseudomonadales", 
"o.Pseudomonadales", "o.Pseudomonadales", "o.Rhodobacterales", 
"o.Rhodobacterales", "o.Rhodobacterales", "o.Rhodobacterales", 
"o.Rhodobacterales", "o.Rhodobacterales", "o.Rhodobacterales", 
"o.Rhodocyclales", "o.Rhodocyclales", "o.Rhodocyclales", "o.Rhodocyclales", 
"o.Rhodocyclales", "o.Rhodocyclales", "o.Rhodocyclales", "o.Rhodospirillales", 
"o.Rhodospirillales", "o.Rhodospirillales", "o.Rhodospirillales", 
"o.Rhodospirillales", "o.Rhodospirillales", "o.Rhodospirillales", 
"o.Sphingomonadales", "o.Sphingomonadales", "o.Sphingomonadales", 
"o.Sphingomonadales", "o.Sphingomonadales", "o.Sphingomonadales", 
"o.Sphingomonadales", "other", "other", "other", "other", "other", 
"other", "other", "o.Syntrophobacterales", "o.Syntrophobacterales", 
"o.Syntrophobacterales", "o.Syntrophobacterales", "o.Syntrophobacterales", 
"o.Syntrophobacterales", "o.Syntrophobacterales", "o.unclassified.Actinobacteria", 
"o.unclassified.Actinobacteria", "o.unclassified.Actinobacteria", 
"o.unclassified.Actinobacteria", "o.unclassified.Actinobacteria", 
"o.unclassified.Actinobacteria", "o.unclassified.Actinobacteria", 
"other", "other", "other", "other", "other", "other", "other", 
"o.Xanthomonadales", "o.Xanthomonadales", "o.Xanthomonadales", 
"o.Xanthomonadales", "o.Xanthomonadales", "o.Xanthomonadales", 
"o.Xanthomonadales", "other", "other", "other", "other", "other", 
"other", "other", "unclassified.Alphaproteobacteria", "unclassified.Alphaproteobacteria", 
"unclassified.Alphaproteobacteria", "unclassified.Alphaproteobacteria", 
"unclassified.Alphaproteobacteria", "unclassified.Alphaproteobacteria", 
"unclassified.Alphaproteobacteria", "unclassified.Bacteroidetes", 
"unclassified.Bacteroidetes", "unclassified.Bacteroidetes", "unclassified.Bacteroidetes", 
"unclassified.Bacteroidetes", "unclassified.Bacteroidetes", "unclassified.Bacteroidetes", 
"unclassified.Deltaproteobacteria", "unclassified.Deltaproteobacteria", 
"unclassified.Deltaproteobacteria", "unclassified.Deltaproteobacteria", 
"unclassified.Deltaproteobacteria", "unclassified.Deltaproteobacteria", 
"unclassified.Deltaproteobacteria", "other", "other", "other", 
"other", "other", "other", "other", "other", "other", "other", 
"other", "other", "other", "other", "other", "other", "other", 
"other", "other", "other", "other", "other", "other", "other", 
"other", "other", "other", "other", "unknown", "unknown", "unknown", 
"unknown", "unknown", "unknown", "unknown"), Percent = c(0.00079334452635543, 
0.00376506929245744, 0.211484817324432, 0.00103385789233346, 
0.000455342754491774, 0.109226673360622, 0.146065614813248, 0.00135779994419094, 
0.00298556484660848, 0.376197178891721, 0.000692440530010073, 
0.000688981301063551, 0.0960669516505888, 0.353112147279037, 
0.386386783179431, 0.233884436467245, 6.1207434989543e-05, 0.655395879965956, 
0.850611230781648, 0.00133397639224432, 0.00282968734708357, 
0.00310219541989955, 0.0555605114544178, 0.125860997421893, 0.0222174828474875, 
0.0018658657934702, 0.174738400794127, 0.0805933832248004, 0.0686635412509354, 
0.0440286429656949, 3.40041305497462e-05, 0.116188228506497, 
0.2214927819849, 0.000658662191444094, 0.00135035887684988, 32.262914349655, 
22.4851536300035, 0.0886604085395998, 28.2891585430014, 37.1954960283737, 
4.02627081614015, 8.50413474937163, 2.57752999456511, 7.4252467908058, 
38.4523679593032, 1.53265953698949, 0.747187213727361, 62.9754630721471, 
33.321436717154, 0.00293395128487204, 0.0083279391598918, 0.173877696948662, 
0.0127869539595679, 0.00203238403234086, 0.058074673794071, 0.149803302489264, 
0.0278803246497321, 0.0426857843504321, 4.51725282958348, 0.00658740390878483, 
0.0043071191533776, 1.60692823291849, 1.93610604243777, 0.0916450035668591, 
0.86232751730846, 0.222868835697402, 0.198120514435449, 0.0229501894095643, 
2.17541925522841, 0.613702831282341, 0.0118953354402953, 0.0662734878104788, 
0.000444915120880516, 0.00426859643259691, 0.00356346049042772, 
0.541294264780554, 0.271320001515608, 0.822727250011829, 0.510125096610364, 
0.0189788170295983, 0.825557379132277, 0.629137641201636, 0.0127719003528049, 
0.0493289342997042, 0.248195408641536, 0.0806278861477308, 0.0214077305925869, 
0.141067584467076, 0.295017073372342, 0.000437658180723202, 0.0135981304701375, 
0.0312941842812499, 0.0195158824169661, 0.00162727401341765, 
0.035912795908312, 0.159622019089816, 0.0011765094281723, 0.00260227249235824, 
0.00297923909331156, 0.00568382388734289, 0.802730133246908, 
0.0129260932945699, 0.00095221263041616, 0.126946222635861, 0.453355107474069, 
1.13545596961548, 4.18899473944914, 8.11766065689633, 2.06054752659127, 
0.842639882613388, 7.12102671310615, 22.821674189625, 0.413521621484032, 
0.451998450356822, 0.00413694532511694, 0.123104526982664, 0.367445316149156, 
0.00404582490507648, 0.0207348769578201, 0.838891628556788, 0.826312742055793, 
0.00216695483856458, 1.56466423073044, 1.06399126582951, 0.230161071031828, 
0.841327016026385, 0.435056818189615, 1.08161572906363, 0.000659141143343916, 
0.37000366175176, 0.235126530038241, 0.00123119224658126, 0.00471189513398425, 
6.09651615560929, 6.24061321184204, 0.0138922845185662, 7.26212399992077, 
4.09705280800074, 0.0592741743850956, 0.0745443605607065, 0.00061895114755415, 
0.00163964091673648, 0.181264880864631, 0.00017248837953624, 
0.000168016742637586, 0.0305321658288366, 0.0937219599092411, 
0.00445319374997175, 0.00645939121920255, 0.792278882962465, 
0.0010484736898646, 0.00147215806283864, 0.160843928666313, 0.430567791473406, 
0.00851934861975876, 0.0535097963230873, 3.88369241019741, 0.000977813653260768, 
0.00315878615041449, 0.315353731475399, 0.384510249904415, 0.00700320441023712, 
0.0161290375409039, 6.80082610994925e-06, 0.0845802781175101, 
0.0224501629719601, 0.000127156283393879, 0.000373034381578614, 
0.882363176798106, 1.23460936713589, 0.00231978716994187, 3.11567847468748, 
1.19474620653331, 0.0051816493345963, 0.0155411785872029, 0.00637058112172389, 
0.017996521725762, 0.00719766980781868, 0.186297286459717, 0.00212201775675165, 
0.0115773270910603, 0.00695380863704964, 0.279960237548975, 0.305153521598013, 
0.00164659851310518, 0.0534353994296546, 0.162760723380343, 0.000437257583008866, 
0.00042346445490608, 0.0177595585668997, 0.0293518014788025, 
0.57001342443674, 0.097449206172384, 0.0370486043732015, 0.0794501640656007, 
0.332996708391615, 0.261317968640289, 0.212483448646628, 0.718291406741452, 
0.0608558846471335, 0.170639794761569, 2.8295497893683, 0.222349188961404, 
0.000502702198615048, 0.00200300158610778, 8.5010326374365e-05, 
0.00105586666464671, 0.000253546149495082, 0.0342072647044998, 
0.0249889601936701, 0.00230470697341575, 0.0151204303346588, 
0.0115818068652435, 0.0027661064461643, 0.000502609791585168, 
0.13548483894405, 0.0612224659542357, 0.0408251270948913, 0.0264451426924254, 
0.000889830241761034, 0.0843684081455349, 0.045969411384351, 
0.00108665868152368, 0.00169604784373673, 0.0511575978053537, 
0.0731041704629772, 0, 0.164268718923243, 0.0749924628105722, 
0.000187941080080568, 0.00025614850384521, 52.9771027463584, 
53.370267792044, 40.6783607030457, 52.9120283573351, 51.5420801524034, 
17.0734338812233, 28.7620673739719)), row.names = c(NA, -238L
), class = "data.frame")

broad plot with broad legend

narrow plot with narrow legend

This is the code I have so far:


ggplotly(
  ggplot(relab.mean.agg, aes(x = Category, y = Percent, fill = Order)) +
  geom_bar(stat = "identity", color = "black") +
  themes[["Dark"]] +
  scale_fill_manual(values = as.vector(polychrome(27)[-c(1,2)]), na.value = "grey",name = NULL) +
  labs(x = "Clusters", y = "Order") +
  theme(text = element_text(size = 20)))%>%
  layout(showlegend=T,height = 500,width = 200+(100*length(unique(relab.mean.agg$Category))) ,legend = list(x = 1, y = 1,orientation = "h",xanchor = "left",yanchor = "top",entrywidth =    0.9,entrywidthmode = "pixels",title = ""))

The entrywidth argument does not change anything...

2
  • What is polychrome? Is it data?
    – Quinten
    Commented Mar 14 at 12:46
  • Ahh sorry. Polychrome is a vector with colors. Its from the "pals" library. Commented Mar 16 at 13:17

0

Browse other questions tagged or ask your own question.