legend

Position legend in first plot of facet

亡梦爱人 提交于 2019-11-29 02:17:56
问题 I would like to put my plot legend inside the plot, inside the first plot of a facet. Here is some example code: df=data.frame( x=runif(10), y=runif(10), facet=rep(c("a","b"),5), color=rep(c("red","blue"),5)) ggplot(data=df,aes(x=x,y=y,color=color))+ geom_point()+ facet_wrap(~facet,ncol=1) Here is the resulting plot: And here is roughly how I would like it to look: Thanks for any help you can provide! 回答1: Assuming your plot is saved as p p + opts( legend.position = c(0.9, 0.6), # c(0,0)

Multiple Lines for Text per Legend Label in ggplot2

我们两清 提交于 2019-11-29 01:48:15
I'm trying to deal with very long labels for a legend in a bar plot (see picture included and the code i used to produce it. I need to break them down in multiple (rows of) lines (2 or 3), other wise the whole picture will be to wide. Some help would be very helpful. Actually I also suspect that my code is not as concise as it should be, but at least it works (but feel free to change) glimpse(df) Observations: 301 Variables: 3 $ V12n <int> 1, 4, 4, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1... $ V12 <fctr> De verwijzer neemt contact op om na te gaa... $ METING.f <fctr> Meting 0, Meting 0, Meting 0,

Change geom_text's default “a” legend to label string itself

老子叫甜甜 提交于 2019-11-29 01:32:46
Similarly to this question , I want to change the default "a" in the legend, but rather than removing it completely, I want to replace it with the labels themselves. That is, the first line of the legend should have a colored icon labeled "se" with the full name "setosa" on the right. iris$abbrev = substr( iris$Species, 1, 2 ) ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape = Species, colour = Species)) + geom_text(aes(label = abbrev)) You can change the legend key generating function. This still requires a bit of manual intervention, but arguably less than using the grobs.

Which CSS tag creates a box like this with title?

别说谁变了你拦得住时间么 提交于 2019-11-29 01:28:08
问题 I want to create a box like this with title: Can any one please let me know if there is a default CSS tag to do this? Or do I need to create my custom style? 回答1: I believe you are looking for the fieldset HTML tag, which you can then style with CSS. E.g., <fieldset style="border: 1px black solid"> <legend style="border: 1px black solid;margin-left: 1em; padding: 0.2em 0.8em ">title</legend> Text within the box <br /> Etc </fieldset> 回答2: If you are not using it in forms, and instead want to

How to position and align a matplotlib figure legend?

给你一囗甜甜゛ 提交于 2019-11-28 21:18:13
I have a figure with two subplots as 2 rows and 1 column. I can add a nice looking figure legend with fig.legend((l1, l2), ['2011', '2012'], loc="lower center", ncol=2, fancybox=True, shadow=True, prop={'size':'small'}) However, this legend is positioned at the center of the figure and not below the center of the axes as I would like to have it. Now, I can obtain my axes coordinates with axbox = ax[1].get_position() and in theory I should be able to position the legend by specifying the loc keyword with a tuple: fig.legend(..., loc=(axbox.x0+0.5*axbox.width, axbox.y0-0.08), ...) This works,

Matplotlib legend, add items across columns instead of down

Deadly 提交于 2019-11-28 21:08:35
For a simple plot below, is there a way to make matplotlib populate the legend so that it fills the rows left to right, instead of first column then second column? >>> from pylab import * >>> x = arange(-2*pi, 2*pi, 0.1) >>> plot(x, sin(x), label='Sine') >>> plot(x, cos(x), label='Cosine') >>> plot(x, arctan(x), label='Inverse tan') >>> legend(loc=9,ncol=2) >>> grid('on') I can think of one possible way. You can order your legend items as you like. All you need to do is to switch the order so that it will give you the result you want. import matplotlib.pyplot as plt import numpy as np import

How to create a scatter plot legend with only one symbol for each label?

雨燕双飞 提交于 2019-11-28 21:03:07
How can I create a scatter plot legend without two symbols showing up each time? I can understand why you'd want this when you're joining symbols by lines, but for a pure scatter plot, all I want in the legend is one example of the symbol. This plot from a previous stackoverflow post shows the kind of thing I mean: In the legend command you can use the scatterpoints option: ax.legend(loc=0, scatterpoints = 1) For a normal plot, it is the option numpoints . Here you can find more information about the keyword arguments for the legend: http://matplotlib.sourceforge.net/api/pyplot_api.html

Manually set color of points in legend

梦想的初衷 提交于 2019-11-28 20:24:49
I'm making a scatter plot which looks like this: (MWE at bottom of question) As can be seen in the image above the colors of the points in the legend are set to blue automatically by matplotlib . I need to set this points to some other color not present in the colormap (ie: black) so they won't generate confusion with the colors associated with said colormap. I looked around but the matplotlib.legend module does not seem to accept a color keyword. Is there any way to do this? Here's the MWE: import matplotlib.pyplot as plt import numpy as np def rand_data(): return np.random.uniform(low=0.,

Cannot add `margin` to `<legend>` element in Safari & Chrome (WebKit)

妖精的绣舞 提交于 2019-11-28 20:24:32
EDIT : As of 2012-06-11 this bug has been finally fixed! https://bugs.webkit.org/show_bug.cgi?id=35981#c1 I have some pretty straightforward markup: <form action=""> <fieldset class="compact"> <legend>Member Tools</legend> <label for="username">Username</label> <input name="username" id="username" type="text"/> <label for="password">Password</label> <input name="password" id="password" type="password" /> </fieldset> </form> I am attempting to add a small margin to the bottom of the legend element, this works just fine in Firefox 2 and 3 as well as IE 5-8, however in Safari and Chrome adding a

ggplot2: how to remove slash from geom_density legend

我们两清 提交于 2019-11-28 20:22:39
I'm trying to plot some overlapping density plots in ggplot2. I'm running into a problem where I cannot remove the diagonal slash from the legend. I have tried using scale_fill_manual() and legend.key as well as the hack from R Cookbook, but I cant seem to get it right. data(iris) iris=iris cols=brewer.pal(3,"Set1") ggplot(iris) + geom_density(position="identity",aes(x=iris$Sepal.Length,fill=cols[1]), colour="black",alpha=.5) + geom_density(position="identity",aes(x=iris$Sepal.Width,fill=cols[2]), colour="black",alpha=.5)+ theme_bw() + scale_fill_identity(guide="legend",labels=c("Sepal Width",