How to make this graph more beautiful?

Dear all, I want to add annotations in scatter plot. But the annotations are very large, how to make it smaller and beautiful?

julia> using DataFrames, RDatasets, Plots

julia> df = dataset("datasets", "iris");

julia> scatter(df.SepalLength, df.SepalWidth, series_annotation = df.Species)

I try to make the annotations smaller, but it does not work

julia> scatter(df.SepalLength, df.SepalWidth, series_annotation = df.Species, annotationfontsize = 5)

Rather than annotating all 150 values, group your data by the specie and then print them in 3 different colours with the species in the legend…

6 Likes

Fyi, StatsPlots is perfect for this:

using StatsPlots
@df df scatter(:SepalLength, :SepalWidth, group=:Species)

But to answer your original question, you can broadcast Plots’ function text() to set the series_annotation attributes:

scatter(df.SepalLength, df.SepalWidth, ms=2, series_annotation = text.(df.Species, :blue, :bottom, 5))
2 Likes

@rafael.guerra Thanks so much. This is what I want. But I have another question. Here is an simply example.

julia> data = rand(10, 2);

julia> df = DataFrame(data, :auto);

julia> city =  ["Shanghai", "Beijing", "Nanjing", "Tangshan", "Tianjin", "Wuhan", "Chongqing", "Heifei", "Hangzhou", "Ningbo"];

julia> insertcols!(df, 1, :city => city);

julia> scatter(df.x1, df.x2, series_annotation = text.(df.city, :magenta, :top, 7), leg = false)

In the graph , I want to adjust “Nanjing” to the right, “Beijing” to the right. and make the text a little far from the marker.

I major in Economics, we often need to add the annotations to the scatter plot.

Can you help me to modify the code?

To change the label positions for a given plot manually you can use the following syntax by providing the annotations coordinates:

scatter(df.x1, df.x2; annotation = (x, y, text.(df.city, :magenta, 6)), leg=false)

Just change x, y around df.x1, df.x2, with the required offsets.

The general problem is very difficult. It would be easier to abbreviate the city names to 2 or 3 letters max and annotate randomly around the points. See this other thread.