0
$\begingroup$

I found the following labeling code (by @Brett) and like to use it as an example:

Module[{
    labels = {"ABC Learning focused", "DEF Positively oriented",
    "GHI Continuous", "KLM Timely", "NOP Clear criteria", 
    "RST Flexible", "UWZ Suited to student level"}, 
data = {8, 6, 4, 5, 5, 9, 9}
}, 
BarChart[data, 
    ChartLabels -> Placed[
        labels, 
        Axis, 
        Block[{text = Rotate[#, (2/7) Pi]}, 
            Row[{text, Invisible[text]}, 
                "\[NegativeMediumSpace]"]
        ]&
        ], 
    PlotRange -> {Automatic, {0, 10}}, Ticks -> {None, Range[0, 10, 2]}, 
    ImagePadding -> {{20, 0}, {95, 0}}
   ]
]

enter image description here

This code would use the labels given in the desired form. My question is how to label the same chart by selecting only the "first 3 letters" (capital or small does not matter) of the existing labels, such as ABC as the label for the first bar in the histogram; DEF as the second label and so on. In fact, such labeling can be generalized by using a rule such as "select CAPITAL letters` only that appear in a label such as those in this example.

For now, I only want to select the first 3 capital letters.

$\endgroup$
2
  • 2
    $\begingroup$ StringTake[#, 3] & /@ labels $\endgroup$ Jan 22, 2020 at 17:59
  • $\begingroup$ @Rohit: Thank you very much for your prompt answer. It works fine. $\endgroup$ Jan 22, 2020 at 18:09

1 Answer 1

4
$\begingroup$

Update - Address comment

Don't know what sectorScores is, but it works fine with the labels from the question.

Module[{labels = {"ABC Learning focused", "DEF Positively oriented", 
    "GHI Continuous", "KLM Timely", "NOP Clear criteria", 
    "RST Flexible", "UWZ Suited to student level"}, 
  data = {8, 6, 4, 5, 5, 9, 9}},
 BarChart[data,
  ChartLabels -> 
   Placed[StringTake[#, 3] & /@ labels, Axis, 
    Block[{text = Rotate[#, (2/7) Pi]}, 
      Row[{text, Invisible[text]}, "\[NegativeMediumSpace]"]] &],
  PlotRange -> {Automatic, {0, 10}},
  Ticks -> {None, Range[0, 10, 2]},
  ImagePadding -> {{20, 0}, {95, 0}}]]

enter image description here


To delete everything except for the capital letters

StringReplace[#, Except[CharacterRange["A", "Z"]] -> ""] & /@ labels

(* {"ABCL", "DEFP", "GHIC", "KLMT", "NOPC", "RSTF", "UWZS"} *)
$\endgroup$
2
  • $\begingroup$ I tried to use your code in the following format: legendsNew = StringTake[#, 3] & /@ sectorScores[[All, 1]]; and ChartLabels -> Placed[legendsNew, {{0.5, 0}, {0.9, 1}}, Rotate[#, (2/7) Pi] &], which uses the entire list of legends for each bar in the chart. In a way, the legends are repearted for each bar, which is not what I want. $\endgroup$ Jan 22, 2020 at 21:13
  • $\begingroup$ Thank you very much for your extensive answer. I found out the answer to my question (given by @kglr at https://mathematica.stackexchange.com/questions/203157/create-inclined-chart-labels-on-stacked-bar-charts). Your code does not have any problem, but I needed to associate the rows of data with labels and there, @kglr gives the answer and worked for me. Thank you so much. $\endgroup$ Jan 22, 2020 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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