Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to place the colorkey legend inside the plot? #487

Closed
montyvesselinov opened this issue Nov 16, 2014 · 6 comments
Closed

How to place the colorkey legend inside the plot? #487

montyvesselinov opened this issue Nov 16, 2014 · 6 comments

Comments

@montyvesselinov
Copy link

How to place the colorkey legend inside the plot? How to control colorkey legend location?

@darwindarak
Copy link
Collaborator

You can adjust the position of the colorkey by setting the key_position argument in the Theme object as :top, :bottom, :left, :right, or :none

plot(x=rand(12), y=rand(12), color=repeat(["a","b","c"], outer=[4], 
     Themes(key_position = :bottom))

To get colors assigned to different data points, you can either reformat your data as a DataFrame

Forces = vcat(
DataFrame(t = t_model, F = data_model, Source = "Model"),
DataFrame(t = t_exp, F = data_exp, Source = "Measured"))

plot(Forces, x = :t, y = :F, color = :Source, Geom.line)

or explicitly specify the color grouping

plot(x = [t_model, t_exp], y = [data_model, data_exp], 
     color = [fill("Model", length(t_model)), fill("Measured", length(t_exp))], 
     Geom.line)

@CorySimon
Copy link

+1
The :right position is I think unacceptable for many journals because of the white space that it wastes. The :top is much better, but still it would be great to be able to put the legends inside the plot, like in matplotlib.

@v-i-s-h
Copy link

v-i-s-h commented Jan 4, 2017

Hi,
when I use :top , entries in the legend gets crowded to left alignment.
Is there any way to nicely spread them out evenly across the horizontal space?

@Mattriks
Copy link
Member

Mattriks commented Jan 4, 2017

using DataFrames, Gadfly, RDatasets
iris = dataset("datasets","iris")

d = Dict{String,String}("setosa"=>"setosa \t","versicolor"=>"versicolor \t", "virginica"=>"virginica \t")
# Add a new variable to the dataframe:
iris[:g] = map(x -> d[x], iris[:Species])

cscale = Scale.color_discrete_manual(colorant"red", colorant"green", colorant"blue")
theme1 = Theme(key_max_columns=3, key_position=:top, guide_title_position=:left)

p = plot(iris, x = :SepalWidth, y = :SepalLength, color = :g, Geom.point, 
    cscale, Guide.colorkey(""),
    theme1
)

issue487a

@Mattriks
Copy link
Member

Mattriks commented Jan 4, 2017

Now back to the initial question in this issue (again using the iris dataset). Note in the code below its possible to use absolute e.g. context(7,6) or relative e.g. context(0.75w, 0.25h) coordinates to locate the key. If using absolute coordinates note the y-coord doesn't match the plot coords, presumably because of the way the key is rendered for 'outside' use.

using Compose
key = Guide.manual_color_key("Iris", ["setosa","versicolor","virginica"], ["red","green","blue"])
# Try changing ctxs[1] to ctxs[2] or ctxs[3] in the line below
# It seems Gadfly automatically renders 3 possible keys
mykey = render(key, Theme(), Gadfly.Aesthetics())[1].ctxs[1]
# Try changing context(7,6) to context(0.75w,0.25h) in the next line
kctx = compose(context(), (context(7,6), mykey))

p = plot(iris, x = :SepalLength, y = :PetalLength, color = :g, Geom.point, 
    cscale, Theme(key_position=:none),
    Guide.annotation(kctx)
)

issue487b

@Mattriks
Copy link
Member

Mattriks commented Jan 5, 2020

This feature was added in #1087.
For discussion on overlapping labels see #859.

@Mattriks Mattriks closed this as completed Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants