I have the following barplot in Julia Plots:
using DataFrames
using Plots
df = DataFrame(group = ["A", "B"],
value = [10, 9])
bar(df[!, "group"], df[!, "value"], legend = :none)
Output:
I would like to show the values of each bar on top of each bar. So in this example, it should show the values 10 for bar A and 9 for bar B. Is there an automatic way for this or should you annotate the values in Julia Plots?
An approach using annotate.
julia> using DataFrames
julia> using Plots
julia> bar(df.group, df.value, legend = :none)
julia> annotate!(df.group, df.value, df.value, :bottom)
I found the argument texts which can show the value on top on the bars like this:
bar(df[!, "group"], df[!, "value"], legend = :none, texts = df[!, "value"])
Output:
As you can see it shows the values, but it isn't nicely aligned.
Related
In R you can add the mean to a boxplot like this:
set.seed(7)
values <- runif(100, 0, 1)
boxplot(values)
points(mean(values), col = 'red')
I would like to do the same in Julia with StatsPlots. Here is the code:
using StatsPlots
boxplot(repeat(['A'],outer=100),randn(300))
Output:
I am not sure how to add a single point (mean) to the boxplot. Is there a similar function like points? So I was wondering if anyone knows how to add the mean to a boxplot in Julia like above?
Adding context to the tip to use scatter!. The difference between scatter and scatter! already has a good explanation here.
julia> using StatsPlots
julia> using Statistics
julia> using Random
julia> Random.seed!(42)
TaskLocalRNG()
julia> val = randn(300);
julia> boxplot(repeat(['A'], outer=100), val, label = "boxplot")
julia> scatter!(['A'], [mean(val)], label = "mean")
If you want a "point" at the mean, you can use a scatter! with the means on top of the boxplots.
I am trying to plot a layout in Julia env using the code below
x = 1:100
y = sin.(rand((Normal()), 100,4))
# plot
plot(x,y)
# layout
plot(x,y, layout = (4,1), color = [:red,:blue])
What I expected was a coloring of each plot with either red or blue. The result was 4 plots that had both red and blue. What am I missing?
It is due to the dimension of color:
julia> [:red,:blue]
2-element Vector{Symbol}:
:red
:blue
It is reasonable to think it will apply this scheme to each 2 items of the original array. However...
julia> [:red :blue]
1×2 Matrix{Symbol}:
:red :blue
this will be applied each 2 columns. So it should be as follows:
julia> plot(x, y, layout=(4, 1), color=[:red :blue])
I am working in R and I have to make many boxplots. This is a visualization of group differences. I want to relabel the x-axis to only have one title instead of five (one for each subplot). My biggest problem is that I also want the y-axis of all the subplots to have different labels.
This is what I tried so far:
par(mfrow=c(1,5))
lapply(NEW8[,c("gawayf", "humf", "sgamesf", "swtoyf", "kissf")],
function(x) boxplot(x ~ NEW8$PAPA_p4_adhd,col=rainbow(2),
names=c("CN","ADHD"),
ylab=c("gawayf", "humf", "sgamesf", "swtoyf", "kissf")))
All the y-labels are added to each subplots so each subplots has 5 lines of y-axis labels (gawayf, humf, sgamef, swtoyf, kissf), and each plot says what data was used to create the boxplot (PAPA_P4_ADHD).
I want each plots to only have the corresponding y-axis label and the x-axis to have 1 label for all five plots.
This is my current output:
Thank you very much
Instead of lapply try mapply - that will allow to pass different argument to each function call:
par(mfrow=c(1,5))
myBox <- function(x, y, ...) boxplot(x ~ y, col=rainbow(2), names=c("CN", "ADHA"), ...)
mapply(myBox,
x = NEW8[,c("gawayf", "humf", "sgamesf", "swtoyf", "kissf")],
y = list(NEW8$PAPA_p4_adhd), # we make this a list so it has length(1)
ylab = c("gawayf", "humf", "sgamesf", "swtoyf", "kissf"),
xlab = "" # empty x-lab
)
For x-lab you will have to do a trick - start a new empty plot that overlays all of the plots, and only add x-axis:
par(fig=c(0,1,0,1), oma=c(0,0,0,0), mar=par("mar"), new=TRUE)
plot.new()
title(xlab="my x-axis")
NOTE: I didn't try to run this code myself, if anything here doesn't work - please leave a comment and will try to address it.
I'm trying to make a bar plot using Plots.jl and the GR backend and wanted to ask how to make the x axis display text labels rather than numbers. Basically this is what I'm doing:
using Plots; gr()
data = [1,2,3]
labels = ["one","two","three"]
bar(data, legend=false)
This produces the following plot:
How do I display my labels ("one", "two", "three"), instead of "1 2 3" on the x axis?
Thanks!
The answer (thanks Tom!) is to pass the labels as x values (currently only possible on the dev branch):
Pkg.checkout("Plots","dev")
using Plots
gr()
data = [1,2,3]
labels = ["one","two","three"]
bar(labels, data, legend=false)
In the Plots version v1.38.0, one can do it by specifying the labels in the optional keyword argument xticks:
data = [1,2,3]
labels = ["one","two","three"]
bar(
data,
legend=false,
xticks=(1:length(data), labels)
)
I have created my bar plot in R and am moving on from using two-bar plots to three-bars.
I want to label the bars '1','2' and '3'along the x-axis
I understand how to use the axis function, and also that the 'at' function requires a numeric value or vector, it's just thatI don't know exactly what value it is that it requires!
Thanks very much in advance
Great news! You can use whatever values you like!
mat <- matrix(c(14,9,7))
barplot(mat[ ,1])
axis(1, at = c(.5,1.5,3.5), labels = 1:3, tick = FALSE, las = 2)