Plot new series based on value within column - plot

Lets assume we have a data like :
1383.77224864627 1910.27446956287 13
2022.08962783969 1870.75968662608 13
2244.59435149955 2334.43843428841 13
3223.73010669775 2409.90834407782 15
708.364446053354 3199.82702811616 15
1190.6942303025 2634.33363259192 15
1749.15599203783 2716.76870380272 19
2227.80906774024 3119.99529267007 19
Is it possible to plot series, based on values in the 3rd column ?
Desired result in this example would be to have 3 series with different colors on one plot.
I'm aware that I can create new series with :
plot 'datafile' using col1:col2, '' using col1:col3
or by using new line between series in datafile but thats not I would like to do.
Thanks !
Edit: I'm using gnuplot version 5.0

Building on my 2012 answer and my understanding of what you want, this should help:
set palette defined ( 13 "blue", 15 "red", 19 "green")
unset key
unset colorbox
plot [500:3500][1800:3500] "x" u 1:2:3 with points palette ps 3 pt 5
which yields
EDIT:
One could also define a continuous palette and let the actual colors be allocated depending upon their value within that scale. In practice:
set palette defined ( 0 "blue", 20 "green", 40 "red")
unset key
set colorbox # for demo purpose
plot [500:3500][1800:3500] "x" u 1:2:3 with points palette ps 3 pt 7
which brings
for the given data set. Colors will change when new series is added.

You would need to filter the datafile first and then plot individual parts separately:
#prepare the command responsible for filtering the data file of interest
#select all rows the value in the 3rd column of which is equal to val
filterData(fname, val)=sprintf("<gawk '$3==%d' %s", val, fname)
#show individual dependencies in one plot
plot for [sval in "13 15"] \
filterData('datafile.dat', int(sval)) w lp t sprintf('value %s', sval)
This would give you:

Related

Assign colors for plotting based on data

I'm trying to plot a scatterplot graph using 13 categories, but when I do it will only give me 8 colors and repeat the same color.
How can I assign 13 different colors based on the category?
plot(combi$solar, combi$aetchem, col=combi$biome, pch=16, xlab="Solar",ylab="AET Chem")
legend("topleft", legend, unique(combi$biome), col=unique(combi$biome), title="Biomes", ncol=3, pch=16)
I'd like to see a scatterplot graph with plotted with 13 different colors based on the category(biome) and matching color legend.
You could create yourself a vector with color names (as many as you want)
colors = c("red","black","blue","yellow","green","purple","pink","brown","darkgreen","grey",
"orange","cyan","darkblue")
Then, give the parameter
col = colors[as.numeric(as.character.factor(combi$biome))]

R plot - different color for defined number of rows

I have a simple data in two columns and 300 rows as:
P1 P2
-0.0319 0.00108078
0.111993 0.000131346
0.117388 -0.0141524
0.117025 0.106717
I have created a simple plot using the very basic plot function:
data <- read.table("data_file", header = TRUE)
plot(data$P1,data$P2, pch=16, col=rainbow(15))
This gave a plot consisting of 300 dots (as number of rows are 300 in data) where the color is repeated after every 15 dots. But I require that the initial 20 dots should appear of same color, then next 20 of other color, and so on. It is pertinent to mention here that these is no category for the data, just to group the dots of plot based on the number of rows.
Thanks for help.
Use rep and colors in case your data increases afterwards.
myCol <- rep(colors()[1:15],each=20) # Define your color Palette
plot(data$P1,data$P2, pch=16, col=myCol) # Use the color Palette
To know more about color list: use colors()and change the above [1:15] to replace your 15 colors.
colors() # The color list having 657 colors
colors()[16:30] # Choose any 15 you require by providing index b/w 1 and 657
colors()[601:615]
You can accomplish this with a call to rep() passing the each argument:
plot(data$P1,data$P2,pch=16L,col=rep(rainbow(15L),each=20L));

R corrplot colorlegend change range

I am trying to plot in R a correlation matrix using the corrplot package.
My problem is that the range of min and max correlation coefficients of the entire matrix is (-0.2,0.2). I plot the matrix with corrplot and I use a custom colorRampPalette, say
col1<-colorRampPalette(c('red','yellow','green','blue'))
for the colormap of the legend, so I set col=col1(10), and I set cl.lim=c(-0.2,0.2).
When I see the plot however the colorlegend appears from -0.2 to 0.2 but with just 2 colors, instead what I would like is a colorlegend with the entire spectrum of colors in 10 bins of the custom palette but in range (-0.2,0.2) so instead of having just 2 colors I will have 10 colors.
The solution for this was duplicate the color range, so, the get the second half...
mypal = jet.colors(1000) # jet.colors from library(matlab)
color = c(mypal,mypal)
corrplot(M, col=color)
I encountered a similar problem but had mostly very high correlations. That made it hard to distinguish between different points without defining a lot of unused colors in my palette.
My solution was to rescale my correlations to the range (-1, 1) (which is the range assumed by corrplot) prior to plotting:
corrplot2 <- function(corr, col) {
a = 2 / (max(corr) - min(corr))
b = 1 - (2 / (1 - (min(corr) / max(corr))))
y = a * corr + b
corrplot(y, method="circle", bg="grey92", col=col,
order="hclust", addrect=4, cl.lim=c(-1, 1))
}
This way the entire distribution of values can again be nicely distinguished using my colors of choice col.

gnuplot: Row stacked bar graph with error bar

I am new to gnuplot. I want to generate graph from data points with three components and standard deviation.
My data looks like this:
TYPE1 15 20 65 5
TYPE2 20 20 60 4
TYPE3 10 30 60 6
TYPE4 30 30 40 5
I want to plot a rowstacked bar for each TYPE with the 3 components stacked and an errobar at the top.
I wrote the following script to do this:
set terminal png
set output "sample.png"
set boxwidth 0.75 relative
set style fill pattern 0 border
set style histogram rowstacked
set style data histograms
set xtics 1000 nomirror
set ytics 100 nomirror
set noytics
set mxtics 2
set mytics 2
set ytics 100
set yrange [0:150]
set ylabel "Y"
set xlabel "X"
set title "Sample graph"
plot 'data.dat' using (100*column(2)/(column(2)+column(3)+column(4))) t "A" , '' using (100*column(3)/(column(2)+column(3)+column(4))) t "B" , '' using (100*column(4)/(column(2)+column(3)+column(4))):xtic(1) t "C"
This produced a graph which looks like this: .
But I am not able to get the errorbar on the top of each bar with deviation values in column 5. I tried different ways using rowstacked and errorbar styled bar graphs but had no luck.
For this you must know, that with the histogram style the boxes are placed at the x-positions 0, 1, etc. i.e. at the row number.
So for the errorbars you must use column(0) as x-coordinate:
set terminal pngcairo
set output "sample.png"
set boxwidth 0.75 relative
set style fill pattern 0 border
set style histogram rowstacked
set style data histograms
set yrange [0:150]
set macros
scale = '100/(column(2)+column(3)+column(4))'
set bars 2.0
plot 'data.dat' using ($2 * #scale):xtic(1) t "A" , \
'' using ($3 * #scale) t "B" , \
'' using ($4 * #scale) t "C",\
'' using 0:(100):5 with errorbars notitle lw 2 lt -1
The result with 4.6.3 is:
For convenience I used a macro scale. The macros work as follows: You define a string, like scale = '...' in the script above. That can be used later in any expression as #scale (you must have set macros enabled). The content of the scale string is then replaced before the respective command is executed.

gnuplot smooth frequency with conditional formatting

Creating a normiles stack diagram works perfectly fine, now I want to use a different color for the boxes fitting in a specific range.
For this, I adopted the following:
set palette maxcolors 2
set palette defined ( 0 '#C0504D', 1 '#00B059')
plot dataFileCity using (rounded(stringcolumn(1) eq city ? $2 : NaN)):
(100 / (bin_width * STATS_records)):($2 > 1300 ? 0 : 1)
smooth frequency with boxes palette
If column 2 has a value highter than 1300 I would like to have a different color.
Which is based on:
Normalized histograms in gnuplot with added function plot
And
Color bars in different colors for some specific values in Gnuplot
However, I am afriad that the smooth frequency makes the thing not work. How can I pass the value such that is creates the a different color?
I know this is nearly 8 years old, but I had the same issue and based on Christoph's comment above I was able to figure out a way.
Below is the graph I wanted:
However selecting certain rows only by way of the ternary and NaN does not play nice with smooth freq, and my histogram was wrong (seemed bins were drawn over one another and frequencies weren't as high as they should've been).
This did not work:
plot \
'filename' using ($1 >= 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "green", \
'filename' using ($1 < 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "red"
In the manual for gnuplot 5.4.2, this section describes an experimental feature which, combined with set table, allowed me to achieve the above graph.
[EXPERIMENTAL] To select only a subset of the data points for tabulation you can provide an input filter condition (if ) at the end of the command. Note that the input filter may reference data columns that are not part of the output. This feature may change substantially before appearing in a released version of gnuplot.
plot <file> using 1:2:($4+$5) with table if (strcol(3) eq "Red")
-- p207 gnuplot v5.4.2 manual
So the approach is:
Use set table $my_data_block_green to set the next plot command to output to the $my_data_block_green data block. We'll create one data block for each colour and this is the first.
Use plot <file> with table if (<condition_for_green>) to write to the green data block only rows matching <condition_for_green>.
Use set table $my_data_block_red (as in point 1).
Use plot <file> with table if (<condition_for_red>) to write to the red data block only rows matching <condition_for_red>.
Cease writing plot commands to tables with unset table.
Plot as normal, referencing the data blocks instead of <file>.
Relevant code (not the full code for graph above):
set table $db1
plot <filename> using 7:(1) with table if ($7 >= 0)
set table $db2
plot <filename> using 7:(1) with table if ($7 < 0)
unset table
plot \
'$db1' using $1:.. .. fillcolor rgb "green", \
'$db2' using $1:.. .. fillcolor rgb "red"
Hope that saves someone a few mins.
Adding to #TKF's answer... There is no need to split the smooth freq data into two tables.
Instead, plot it into one table and set the color via lc rgb variable and by defining an appropriate function.
The following example works for gnuplot>=5.2, with some modifications also with earlier versions.
Code:
### histogram with split colors
reset session
# create some random test data
set print $Data
do for [i=1:2000] {
print sprintf("%g",int(invnorm(rand(0))*100))
}
set print
stats $Data u 1 nooutput
xmin = STATS_min
xmax = STATS_max
N = 20
myWidth = (xmax-xmin)/N
bin(col) = myWidth*floor(column(col)/myWidth)+myWidth/2.
set boxwidth myWidth
set key noautotitle
set style fill solid 0.3
set grid x,y
set table $Histo
plot $Data u (bin(1)) smooth freq
unset table
myColor(col) = column(col)<0 ? 0xff0000 : 0x00cc00
plot $Histo u 1:2:(myColor(1)) w boxes lc rgb var
### end of code
Result:

Resources