9
$\begingroup$

This is my data

mydata = {{0.55, 31.7, 26.360, "acid 0-TOLUIC"}, {0.3925, 31.503, 
31.4861, "acid 2- methyloctanoic"}, {0.711, 11.299, 17.4735, 
"acid 6-hydroxyhexanoic"}, {0.495, 27.57, 30.437, 
"acid acetic"}, {0.506, 49.6, 35.486, 
"acid acetoxyacetic"}, {0.753, 17.37, 13.3028, 
"acid acetylsalicylic"}, {0.873, 5.576, 5.325, 
"acid azelaic"}, {0.652, 17.968, 17.040, 
"acid cyclopentylacetic"}, {0.471, 33.9659, 33.384, 
"acid glutaric"}, {0.753, 26.96, 20.881, 
"acid isophtalic"}, {0.673, 22.433, 26.411, 
"acid maleic"}, {0.627, 25.7, 21.504, "acid m-toluic"}, {0.749, 
12.187, 10.113, "acid neohexanoic"}, {0.5512, 20.3642, 22.555, 
"acid n-nonanoic"}, {0.802, 7.629, 8.243, 
"acid n-undecanoic"}, {0.830, 9.426, 10.141, 
"acid phtalic"}, {0.864, 8.713, 7.9158, "acid salycilic"}};

I want to plot the data with different colors depending to name of the data. So, if the data are (e.g. "acid n-undecanoic" or "acid salycilic") in a particular way I want them in red otherwise in green. Following my not working code.

ListPlot[{mydata[[All, 2]], mydata[[All, 1]]}\[Transpose], 
PlotStyle -> {If[
mydata[[All, -1]] == "acid n-undecanoic" || 
mydata[[All, -1]] == "acid salycilic", Red, Green]}]
$\endgroup$
6
$\begingroup$
ListPlot[List /@ mydata[[;; , {2, 1}]], BaseStyle -> PointSize@.02, PlotRange -> Full,
 PlotStyle -> Replace[mydata[[;; , -1]], 
                      {"acid n-undecanoic" | "acid salycilic" -> Red, _ -> Green},
                      {1}]
 ]
$\endgroup$
  • $\begingroup$ So it is just plotting not one set of points but many 1-point sets. $\endgroup$ – Kuba Sep 16 '14 at 12:28
  • $\begingroup$ It works! Thanks a lot... $\endgroup$ – Mary Sep 16 '14 at 12:48
5
$\begingroup$

Here is a simple step-by-step approach.

sorted = Sort @ mydata;
pts = List /@ sorted[[All, ;; 2]];
colors = 
  If[StringMatchQ[#, "acid n-undecanoic" | "acid salycilic"], Red, Green] & /@ 
    sorted[[All, -1]];
ListPlot[pts, PlotStyle -> colors]

plot

Update

The above plot would really be more useful if it had tooltips identifying which acid a point represented on mouse-over. Such tooltips are really easy to add. Here is the code showing how to add them:

Module[{sorted, pts, names, colors},
  sorted = Sort @ myData;
  pts = List /@ sorted[[All, ;; 2]];
  names = sorted[[All, -1]];
  colors = 
    If[StringMatchQ[#, "acid n-undecanoic" | "acid salycilic"], Red, Green] & /@ names;
  ListPlot[Thread[Tooltip[pts, names]], PlotStyle -> colors]]

tooltips

$\endgroup$
  • $\begingroup$ Very smart solution...Thanks a lot! $\endgroup$ – Mary Sep 16 '14 at 12:52
3
$\begingroup$

An alternative using Graphics

acids[data_, choice_] := Module[{points},

  points =
   data /. {a_, b_, c_, d_} :>
     If[Or @@ choice /. s_String :> d == s,
      {Text[d, {b, a - 0.025}], {Red, Point[{b, a}]}},
      {Blue, Point[{b, a}]}];

  Graphics[{PointSize[0.02], points},
   AspectRatio -> 1/GoldenRatio,
   Axes -> True,
   AxesOrigin -> {0, 0.3},
   ImageSize -> 400]]

acids[mydata, {"acid glutaric", "acid acetic", "acid azelaic"}]

enter image description here

$\endgroup$
3
$\begingroup$

An alternative approach is Styleing individual points in the data set and using the styled data set as the input to ListPlot. Define a coloring function:

colorF["acid n-undecanoic" | "acid salycilic"] = Red;
colorF[_] := Green;

styleddata = Style[{#2, #1}, colorF[#4], PointSize@.02] & @@@ mydata;
ListPlot[styleddata]

enter image description here

Note: We can also use colorF to set the option value for PlotStyle after breaking mydata into Length[mydata] data sets as in Kuba's approach:

ListPlot[List /@ mydata[[All, {2, 1}]], 
         BaseStyle -> PointSize@.02, 
         PlotStyle -> (colorF /@ mydata[[All, -1]])]

enter image description here

Since we do not break the data into individual points or change the ordering of points in the data set while styling, we can use the option Joined -> True directly, when we need to, without additional processing:

ListPlot[styleddata, Joined->True, PlotMarkers->{Automatic, 0}]

enter image description here

Update: A function similar to colorF can be used for labeling select points (Thanks @eldo for raising the bar:)

labelF[a : "acid n-undecanoic" | "acid salycilic"] := a;
labelF[_] := "";
styleddata = Style[Labeled[{#2, #1}, labelF@#4, Below], colorF@#4, PointSize@.02] & @@@ mydata;
ListPlot[styleddata]

enter image description here

$\endgroup$
  • $\begingroup$ That's exceptional! $\endgroup$ – Mary Sep 16 '14 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.